为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

曹江华 2016-11-27 15:52:57
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
查看完整描述

3 回答

?
曹江华

TA贡献1条经验 获得超0个赞

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();
	}
	}
}


查看完整回答
反对 回复 2016-11-28
?
慕粉3291149

TA贡献71条经验 获得超52个赞

代码贴一下啊

查看完整回答
反对 回复 2016-11-28
  • 3 回答
  • 0 关注
  • 3057 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信