2

Consider that I have a standard DOMParser, written along the lines of:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder(); factory.newDocumentBuilder().newDocument();
    this.document = builder.parse(new InputSource(new StringReader(xmlString)));

Then when I feed this, it works:

<?xml version="1.0" encoding="utf-8" standalone="no"?><rsp stat="ok">
<photos page="1" pages="385628" perpage="10" total="3856272">
    <photo farm="7" id="5943736412" isfamily="0" isfriend="0" ispublic="1" owner="35783591@N08" secret="7c3ee3a0ee" server="6144" title="SOOC"/>
    <photo farm="7" id="5943736298" isfamily="0" isfriend="0" ispublic="1" owner="51692758@N06" secret="3a6d0657e0" server="6130" title="Large Express Orange Cami with Lace Trim $1"/>
    <photo farm="7" id="5943180023" isfamily="0" isfriend="0" ispublic="1" owner="67953162@N00" secret="36177b9be7" server="6022" title="little fellow at the heart"/>
    <photo farm="7" id="5943741338" isfamily="0" isfriend="0" ispublic="1" owner="9121546@N05" secret="f5dcb26ba0" server="6025" title="Freeway Drive"/>
    <photo farm="7" id="5943182403" isfamily="0" isfriend="0" ispublic="1" owner="62760035@N06" secret="1a77e9ea6f" server="6131" title="Mk1_Orange"/>
    <photo farm="7" id="5943159235" isfamily="0" isfriend="0" ispublic="1" owner="63335898@N06" secret="cd80b51040" server="6027" title="D'CLOSET; GD1181 (97% Rayon 3% Spandex) Navy, Black, Dark Orange ~45.5 in. +55.5 in. $16.25 01-3."/>
    <photo farm="7" id="5943150801" isfamily="0" isfriend="0" ispublic="1" owner="49610142@N03" secret="d22b924c36" server="6124" title="orange_hat5"/>
    <photo farm="7" id="5943149751" isfamily="0" isfriend="0" ispublic="1" owner="93794898@N00" secret="45a4d3681f" server="6137" title="Southern White Faced (Scops) Owl (Ptilopsis granti)"/>
    <photo farm="7" id="5943147771" isfamily="0" isfriend="0" ispublic="1" owner="64331041@N07" secret="b79de60d1a" server="6148" title="Another shot of the orange #VW #Beetle everybody loved so much :)"/>
    <photo farm="7" id="5943169451" isfamily="0" isfriend="0" ispublic="1" owner="25656513@N06" secret="3137cccc19" server="6128" title="365:26 - False Coloured"/>
</photos>
</rsp>

But when I feed this, it doesn’t parse, and throws the exception shown below:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photos page="1" pages="385871" perpage="10" total="3858702">
    <photo id="5943232363" owner="65158874@N02" secret="ed3a8c8859" server="6142" farm="7" title="Sunset" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943241319" owner="65158874@N02" secret="3eeefe774d" server="6018" farm="7" title="Sunset" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943783494" owner="65158874@N02" secret="40488c323f" server="6142" farm="7" title="Sunset" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943788400" owner="65158874@N02" secret="a71e479368" server="6003" farm="7" title="Sunset Storm Clouds" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943238975" owner="65158874@N02" secret="eebcee0087" server="6148" farm="7" title="Clouds of Fire" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943240733" owner="65158874@N02" secret="13b8236166" server="6024" farm="7" title="Clouds of Fire" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943233445" owner="65158874@N02" secret="9ab47a506e" server="6001" farm="7" title="Sunset Storm Clouds" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="4866746451" owner="51206279@N07" secret="4527be5fcd" server="4121" farm="5" title="alive" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943234465" owner="65158874@N02" secret="600d8af8ee" server="6141" farm="7" title="Sunset Storm Clouds" ispublic="1" isfriend="0" isfamily="0" />
    <photo id="5943243347" owner="65158874@N02" secret="de74666e23" server="6029" farm="7" title="Moon" ispublic="1" isfriend="0" isfamily="0" />
</photos>
</rsp>

The exception thrown is:

[Fatal Error] :-1:-1: Premature end of file.
Exception in thread "main" 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)

Any ideas would be much appreciated!

2
  • 1
    How is the xmlString variable being populated? Commented Jul 16, 2011 at 18:08
  • The first I am loading from a file. Transforming to a string, lets call it ‘xmlString1’. Then attempting to re-parse. The second is obtained by making a RESTful call to an api. Commented Jul 16, 2011 at 18:12

1 Answer 1

1

I can parse both your examples fine with your code (modified to read the XML from a file that I copy/pasted your examples into, not a string).

My best guess is that there's chars in the string that are not copied over in the copy-paste you've done.

UPDATE - if you are accessing a resource over HTTP, there are things that can be going wrong. There's some anecdotal evidence that even the HTTP content-length header can cause the problem you're seeing, for example. I suggest you look carefully at the HTTP side of things, maybe even post up the code you're using to obtain the document, and the document URL if it's a public resource.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. @Larry: What happens if you open the original XML files in a reader understanding XML (e.g. Chrome, Firefox). Maybe there are some control character in the original file?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.