package lei3;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Schoolread {
public void school(){
File file = new File("c:/school.xml");
SAXReader saxreader = new SAXReader();
try {
Document document = saxreader.read(file);
Element rootElement = document.getRootElement();
String rootname = rootElement.getName();
System.out.println("根节点:"+rootname);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Schoolread sr = new Schoolread();
sr.school();
}
}
报错如下:
org.dom4j.DocumentException: Error on line 5 of document file:///c:/school.xml : The content of elements must consist of well-formed character data or markup. Nested exception: The content of elements must consist of well-formed character data or markup.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.io.SAXReader.read(SAXReader.java:264)
at lei3.Schoolread.school(Schoolread.java:16)
at lei3.Schoolread.main(Schoolread.java:31)
Nested exception:
org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1438)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1723)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:264)
at lei3.Schoolread.school(Schoolread.java:16)
at lei3.Schoolread.main(Schoolread.java:31)
Nested exception: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1438)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1723)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.io.SAXReader.read(SAXReader.java:264)
at lei3.Schoolread.school(Schoolread.java:16)
at lei3.Schoolread.main(Schoolread.java:31)
5 个解决方案
#1
school.xml 贴出来
#2
school.xml 这个文件出错了
#3
school代码:
<?xml version="1.0" encoding="UTF-8"?>
<school name="xingxiangaozhong" xingzhi="gongli">
<!--zengjiazhushi-->
<gaoyi name="gaoyi">
<191/>
<192/>
<193/>
</gaoyi>
<gaoer name="gaoer"/>
<gaosan name="gaosan"/>
</school>
<?xml version="1.0" encoding="UTF-8"?>
<school name="xingxiangaozhong" xingzhi="gongli">
<!--zengjiazhushi-->
<gaoyi name="gaoyi">
<191/>
<192/>
<193/>
</gaoyi>
<gaoer name="gaoer"/>
<gaosan name="gaosan"/>
</school>
#4
问题出在第五行 <191 />
参考:http://www.w3school.com.cn/xml/xml_elements.asp
参考:http://www.w3school.com.cn/xml/xml_elements.asp
XML 命名规则
XML 元素必须遵循以下命名规则:
名称可以含字母、数字以及其他的字符
名称不能以数字或者标点符号开始
名称不能以字符 “xml”(或者 XML、Xml)开始
名称不能包含空格
可使用任何名称,没有保留的字词。
#5
#1
school.xml 贴出来
#2
school.xml 这个文件出错了
#3
school代码:
<?xml version="1.0" encoding="UTF-8"?>
<school name="xingxiangaozhong" xingzhi="gongli">
<!--zengjiazhushi-->
<gaoyi name="gaoyi">
<191/>
<192/>
<193/>
</gaoyi>
<gaoer name="gaoer"/>
<gaosan name="gaosan"/>
</school>
<?xml version="1.0" encoding="UTF-8"?>
<school name="xingxiangaozhong" xingzhi="gongli">
<!--zengjiazhushi-->
<gaoyi name="gaoyi">
<191/>
<192/>
<193/>
</gaoyi>
<gaoer name="gaoer"/>
<gaosan name="gaosan"/>
</school>
#4
问题出在第五行 <191 />
参考:http://www.w3school.com.cn/xml/xml_elements.asp
参考:http://www.w3school.com.cn/xml/xml_elements.asp
XML 命名规则
XML 元素必须遵循以下命名规则:
名称可以含字母、数字以及其他的字符
名称不能以数字或者标点符号开始
名称不能以字符 “xml”(或者 XML、Xml)开始
名称不能包含空格
可使用任何名称,没有保留的字词。