Problem parsing String to DOM in Java -
Problem parsing String to DOM in Java -
consider have standard domparser, written along lines of:
documentbuilderfactory mill = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); factory.newdocumentbuilder().newdocument(); this.document = builder.parse(new inputsource(new stringreader(xmlstring)));
then when feed this, 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 lace trim $1"/> <photo farm="7" id="5943180023" isfamily="0" isfriend="0" ispublic="1" owner="67953162@n00" secret="36177b9be7" server="6022" title="little fellow @ 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 orange #vw #beetle loved 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 feed this, doesn’t parse, , throws 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. @ org.apache.xerces.parsers.domparser.parse(unknown source) @ org.apache.xerces.jaxp.documentbuilderimpl.parse(unknown source)
any ideas much appreciated!
i can parse both examples fine code (modified read xml file copy/pasted examples into, not string).
my best guess there's chars in string not copied on in copy-paste you've done.
update - if accessing resource on http, there things can going wrong. there's some anecdotal evidence http content-length header can cause problem you're seeing, example. suggest @ http side of things, maybe post code you're using obtain document, , document url if it's public resource.
java dom xml-parsing
Comments
Post a Comment