org.xml.sax.SAXParseException:prolog中不允许使用内容

时间:2012-07-23 17:11:15

标签: java xml dom domparser

我正在尝试编写一个逐行解析XML文件的DOM Parser,但是我收到的错误如下:

org.xml.sax.SAXParseException: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
    at ParseXML.main(ParseXML.java:30)
Exception in thread "main" java.lang.NullPointerException
    at ParseXML.main(ParseXML.java:38)

我的java代码如下:

public class ParseXML {
    public static void main(String[]args)
    {
        File xmlfile= new File("C:\\Users\\schhatre\\XMLReports");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = dBuilder.parse(xmlfile);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        doc.getDocumentElement().normalize();
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    }


}

XML文件看起来像这样:

<?xml version="1.0"?>
<suiteResults>
  <FitNesseVersion>v20110104</FitNesseVersion>
  <rootPath>MoxyInterfaceStandAloneSuite</rootPath>
    <pageHistoryReference>
    <name>RcopiaWeb.FunctionalTestSuite.MoxySuite.MoxyInterfaceSuite.MoxyInterfaceStandAloneSuite.SuiteSetUp</name>
    <date>07/06/2012 14:16:52</date>
    <pageHistoryLink>RcopiaWeb.FunctionalTestSuite.MoxySuite.MoxyInterfaceSuite.MoxyInterfaceStandAloneSuite.SuiteSetUp?pageHistory&amp;resultDate=20120706141652</pageHistoryLink>
    <counts>
      <right>0</right>
      <wrong>0</wrong>
      <ignores>0</ignores>
      <exceptions>10</exceptions>
    </counts>
    <runTimeInMillis>2609</runTimeInMillis>
  </pageHistoryReference>

0 个答案:

没有答案