在Java中将字符串解析为DOM时出现问题
考虑一下我有一个标准的 DOMParser,按照以下方式编写:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); factory.newDocumentBuilder().newDocument();
this.document = builder.parse(new InputSource(new StringReader(xmlString)));
然后当我提供这个时,它可以工作:
<?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>
但是当我提供这个时,它不会解析,并抛出如下所示的异常:
<?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>
抛出的异常是:
[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)
任何想法都会非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以使用您的代码很好地解析您的两个示例(修改为从我将示例复制/粘贴到的文件中读取 XML,而不是字符串)。
我最好的猜测是,字符串中的某些字符在您完成的复制粘贴中没有被复制。
更新 - 如果您通过 HTTP 访问资源,则可能会出现问题。有一些轶事证据表明,即使是 HTTP 内容长度标头也可能导致您所看到的问题,例如。我建议您仔细查看 HTTP 方面的内容,甚至可以发布您用于获取文档的代码,以及文档 URL(如果它是公共资源)。
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.