Dave
Dave

Reputation: 19320

org.xml.sax.SAXParseException: Premature end of file

I'm using Java 6, trying to parse this namespace-less XML ...

<gen type='section' name='Gen Info'>     
     <pmult type='input' name='Price Multiplier' nwidth='200' vlength='10'>
        1000.0 
     </pmult>
     <tunit type='input' name='Trading Unit' nwidth='200' vlength='10'>
        BBL
     </tunit>
     <tmsure type='input' name='Trading Measure' nwidth='200' vlength='10'>
        1000.0
     </tmsure>
</gen>

and getting this error ...

org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
at com.cme.clearing.commons.xmlparser.XMLParser.parse(XMLParser.java:53)
at com.cme.clearing.commons.xmlparser.XMLParser.parse(XMLParser.java:35)
at com.cme.clearing.commons.xmlparser.XMLParser.parse(XMLParser.java:80)
at com.cme.clearing.commons.xmlparser.XMLParser.parse(XMLParser.java:35)
at com.cme.clearing.commons.xml.DNodeTest.testParsingNodeWithChildNodes(DNodeTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Below is the Java code I'm using to parse it ...

public static com.cme.clearing.commons.xml.Node parse(String xml) throws 
        ParserConfigurationException, SAXException, IOException {
    xml = xml.trim();
    return parse(new ByteArrayInputStream(xml.getBytes()));
}

public static com.cme.clearing.commons.xml.Node parse(final InputStream xmlInputStream)
        throws ParserConfigurationException,
        SAXException, IOException {

    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    final DocumentBuilder db = dbf.newDocumentBuilder();
    final Document doc = db.parse(xmlInputStream);

Any ideas what's malformed about the XML or what's wrong with the parsing logic? - Dave

Upvotes: 2

Views: 9369

Answers (1)

user785262
user785262

Reputation:

I think it is possibly a bug in the SAXParser in that version. This is the same problem I ran into with IBM's JDK. I ended up using the -Xbootclasspath option to drop in a different version of the SAXParser (download xerces.jar and xml-api.jar) in order to solve the problem. Huge pain, but it works.

Upvotes: 1

Related Questions