SAX读取XML报错

前端之家收集整理的这篇文章主要介绍了SAX读取XML报错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

控制台错误

[Fatal Error] :7:6: The string "--" is not permitted within comments.

Exception in thread "main" org.xml.sax.SAXParseException: The string "--" is not permitted within comments.

at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)

at org.email.SaxTest.main(SaxTest.java:21)

java代码

package org.email;

import java.io.FileReader;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.helpers.XMLReaderFactory;

public class SaxReadXml extends DefaultHandler {

public static void main(String[] args) throws ParserConfigurationException,

SAXException,IOException {

XMLReader xr = XMLReaderFactory

.createXMLReader("org.apache.xerces.parsers.SAXParser");

xr.setContentHandler(new SaxReadXml());

xr.parse(new InputSource(new FileReader(

"D://WORK//SendEmail//src//Config.xml")));

}

public void characters(char[] ch,int start,int length)

throws SAXException {

String data = new String(ch,start,length);

System.out.println("E-mail" + data);

}

}

XML文档:

<?xml version="1.0" encoding="UTF-8"?>

<memory>

<!--王小林-->

<email>

<receiver>81401592@qq.com</receiver>

</email>

<!-- me -->

<email>

<receiver>1058106015@qq.com</receiver>

</email>

</memory>

原因分析:注意此处注释:<!--王小林-->与下方得区别<!-- me -->

王小林后面少了一个空格。

猜你在找的XML相关文章