1

I am trying to parse an xml using DocumentBuilder's parse method which takes URI as an argument. For this I create a String object representing the URI and then call the parse method passing the String object as the argument.

The parse method call works fine, returns a new DOM object. However, when I try to print the returned DOM object, it says: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed. The code snippet for parsing the xml is as shown below:

String sURL="http://host:port/myapp/serv?arg=abc&arg2=def";  
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = dBuilder.parse(sURL);

When I manually try accessing the URL in a browser it works fine, i.e it returns the xml.

The xml that the url returns is well formed.

The parse method when called as shown above doesn't throw any exception, however the DOM object tells about org.xml.sax.SAXParseException

Below is the stacktrace of the exception:

org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at DocBuilderTest.getDocumentFromURL(DocBuilderTest.java:103) at DocBuilderTest.main(DocBuilderTest.java:54)

Any help on this will be highly appreciated.

Thanks.

ria
  • 7,904
  • 11
  • 39
  • 46
  • 1
    Create an XML document in Eclipse with the contents of your document returned by the server and validate it to see what's wrong with the document – Timmo Dec 14 '11 at 09:08
  • I tried the same... the xml got validated successfully... however the DOM object that the parse method returns says "org.xml.sax.SAXParseException" – ria Dec 14 '11 at 09:15
  • Please post the stack trace of the DOM exception. – OldCurmudgeon Dec 14 '11 at 09:24
  • check http://stackoverflow.com/questions/5742543/an-invalid-xml-character-unicode-0xc-was-found similar question i hope it will work – Ashish Dec 14 '11 at 10:52
  • Have added the stack trace of the exception. – ria Dec 14 '11 at 11:29
  • 2
    Apparently the XML document is not well-formed after all. Can you show it to us? – mzjn Dec 14 '11 at 11:48

0 Answers0