Java解析xml文件时会抛出“The encoding declaration is required in the text declaration.”

org.jdom2.input.JDOMParseException: Error on line 1: The encoding declaration is required in the text declaration.

at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232)

at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:253)

at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1091)

at com.imooc.jdomtest.JDOMTest.main(JDOMTest.java:26)

Caused by: org.xml.sax.SAXParseException: The encoding declaration is required in the text declaration.

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)

at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)

at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanXMLDeclOrTextDecl(XMLScanner.java:515)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanXMLDeclOrTextDecl(XMLDocumentFragmentScannerImpl.java:951)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$XMLDeclDriver.next(XMLDocumentScannerImpl.java:815)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)

at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:452)

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)

at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)

at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)

at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)

at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)

... 3 more


曹江华
浏览 3058回答 3
3回答

曹江华

package com.imooc.jdomtest; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import com.imooc.entity.Book; public class JDOMTest { /**  * @param args  */ public static void main(String[] args) { //      SAXBuilder saxBuilder=new SAXBuilder();      //创建一个输入流      InputStream in; try {  in = new FileInputStream("src/res/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){          Book bookEntity=new Book();           System.out.println("====开始解析第"+(bookList.indexOf(book)+1)+"本书===");           List<Attribute>  attrList=book.getAttributes();           for(Attribute attr :attrList){           String attrName=attr.getName();           String attrValue =attr.getValue();           System.out.println("属性名:"+attrName+"----属性值"+attrValue);          if(attrName.equals("id")){           bookEntity.setId(attrValue);          }            }          List<Element>  bookChilds= book.getChildren();          for(Element child :bookChilds){          System.out.println("节点名"+child.getName()+"-----节点值"+child.getValue());          }           System.out.println("====结束解析第"+(bookList.indexOf(book)+1)+"本书===");                     } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

慕粉3291149

代码贴一下啊
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java