在Java中解析String到DOM的问题

时间:2011-07-16 17:52:45

标签: java dom xml-parsing

考虑到我有一个标准的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)

任何想法都会非常感激!

1 个答案:

答案 0 :(得分:1)

我可以使用您的代码解析您的示例(修改为从我复制/粘贴示例的文件中读取XML,而不是字符串)。

我最好的猜测是,字符串中的字符未在您完成的复制粘贴中复制。

更新 - 如果您通过HTTP访问资源,有些事情可能会出错。例如,有some anecdotal evidence甚至HTTP内容长度标头也可能导致您遇到的问题。我建议你仔细查看HTTP方面的内容,甚至可能会发布用于获取文档的代码,以及文档URL(如果它是公共资源)。