隔壁王爷爷
2015-03-31 14:13
package com.imooc.jdomtest; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import java.io.*; import java.util.List; /** * Created with IntelliJ IDEA. * User: QiuShiLe * Date: 2015/3/26 * Time: 19:37 * Project: XMLAnalyse */ public class JDOMTest { public static void main(String[] args) { //创建一个SAXBuilder对象 SAXBuilder saxBuilder = new SAXBuilder(); //创建一个输入流 以加载xml InputStream in; try { in = new FileInputStream("demo" + java.io.File.separator + "books.xml"); InputStreamReader isr = new InputStreamReader(in, "UTF-8"); Document document = saxBuilder.build(isr); //获取根节点 Element rootElement = document.getRootElement(); //获取子节点的集合 List<Element> bookList = rootElement.getChildren(); for (Element book : bookList) { System.out.println("===开始解析第" + (bookList.indexOf(book) + 1) + "本书==="); //解析book的属性 //book.getAttributeValue("id"); //知道名称时获取节点值 List<Attribute> attrList = book.getAttributes(); for (Attribute attr : attrList) { System.out.println(attr.getName()); System.out.println(attr.getValue()); } List<Element> bookChilds = book.getChildren(); for (Element child : bookChilds) { System.out.println("节点" + child.getName() + "的值为" + child.getValue()); } // // //对book的子节点及节点值遍历 // for (Element child : book.getChildren()) { // System.out.println("节点 " + child.getName() + " 的值为 " + child.getValue()); // } System.out.println("===结束解析第" + (bookList.indexOf(book) + 1) + "本书==="); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
"C:\Program Files\Java\jdk1.8.0_20\bin\java" -Didea.launcher.port=7535 "-Didea.launcher.bin.path=D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_20\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_20\jre\lib\ext\zipfs.jar;D:\WorkStation\IntelliJ\XMLAnalyse\out\production\XMLAnalyse;D:\Downloads\jdom-2.0.6\jdom-2.0.6.jar;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.imooc.jdomtest.JDOMTest org.jdom2.input.JDOMParseException: Error on line 1: 前言中不允许有内容。 at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:303) at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1196) at com.imooc.jdomtest.JDOMTest.main(JDOMTest.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217) ... 8 more Process finished with exit code 0
应该是编码错误,你看下xml编码是不是utf-8。因为识别不出XML,才会报前言不能有内容
我也是出现了这个问题 修改了编码就好了 原本的编码是 UTF-8 with BOM
你使用Editor打开,将文件保存为无BOM头的UTF-8格式,类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM),这个应该就是指前沿中的内容,只要去掉就可以执行了。
你的xml文件出现错误了,<?xml>中间没有空格,如果有空格的话会出现错误,如果是编码问题的话,打印出来是乱码,而不是这个错误。
应该是读取的xml文件包含BOM头,我用Sublime打开 -> 文件 -> 保存编码 -> UTF-8 就好了
请问“前言不能有内容”这个问题你解决了吗?,代码和老师的一样,但是还会报错
隔壁老邱你好,隔壁老邱再见!
这是原图
Java眼中的XML---文件读取
83199 学习 · 431 问题
相似问题