2

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

Dave
  • 15,639
  • 133
  • 442
  • 830
  • What is `com.cme.clearing.commons.xml.Node` ? It should be `org.w3c.dom.Node`. – Ludovic Kuty Oct 20 '11 at 18:51
  • This code parse this xml successfully on my side (sun jdk 1.6.0_16). – svaor Oct 20 '11 at 18:54
  • Maybe you have to use double quotes instead of single quotes for your attributes. – Michael Oct 20 '11 at 19:09
  • @Dave Why don't you use the included DOM parser in the JDK, aka Xerces Java 2 ? – Ludovic Kuty Oct 20 '11 at 19:31
  • Michael, no single quotes are fine. Ludovic: Apache Xerces is generally more reliable than the version of Xerces found in the JDK. – Michael Kay Oct 20 '11 at 21:13
  • 1
    I don't think there's anything wrong with the XML or Java that you've shown us. Which means there's something wrong somewhere else. – Michael Kay Oct 20 '11 at 21:14
  • Is there a way to close or "withdraw" these questions? I gave some incomplete information, which is why its not possible to answer this ? in the current form. – Dave Oct 24 '11 at 16:14

1 Answers1

1

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.