目标 C:解析格式错误的 XML

发布于 2024-11-06 08:52:20 字数 3700 浏览 0 评论 0原文

我正在编写一个从 Flickr API 下载数据的 iPhone 应用程序。目前似乎无法限制评论的数量< /a> 它会下载,虽然我想下载 8 或 10 个,但有时它会给我发送数百个。我有子类ASIHTTPRequest 这样它只会下载设定数量的字节(例如,收到后会停止下载) 1024 字节注释数据)。

现在,我想要解析的信息都已存在(评论数据包含用户 ID、文本等内容)。但是,由于它在结束之前被切断,因此 XML 格式错误,并且我当前的解决方案(使用 ObjectiveFlickr 的 XML 解析器)无法解析 XML。有没有一种方法可以处理格式错误的 XML,就像旧的 Web 浏览器处理 HTML 的方式一样,并且只提取格式良好的数据?

这是一些示例数据:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<comments photo_id="5692627867">
    <comment id="49862655-5692627867-72157626659891768" author="29114051@N05" authorname="eαse*" iconserver="4046" iconfarm="5" datecreate="1304689286" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626659891768">lovely lovely lovelyyyyy!!! ♥♥♥♥♥♥♥♥♥</comment>
    <comment id="49862655-5692627867-72157626535581359" author="49946698@N06" authorname="RandomPics Art" iconserver="4017" iconfarm="5" datecreate="1304692593" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626535581359">nice! like soft colors and tones!</comment>
    <comment id="49862655-5692627867-72157626660240896" author="49907977@N06" authorname="kiki_chi" iconserver="4014" iconfarm="5" datecreate="1304693051" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660240896">&amp;gt;eαse*
&amp;gt;RandomPics Art

Thank you:) :) :)</comment>
    <comment id="49862655-5692627867-72157626660761230" author="41717031@N08" authorname="petia.bourova" iconserver="4082" iconfarm="5" datecreate="1304698244" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660761230">Thank you!Very nice photo!I like The coulers very,very much!!!</comment>
    <comment id="49862655-5692627867-72157626661258700" author="31540474@N08" authorname="Leentje32" iconserver="4067" iconfarm="5" datecreate="1304703576" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626661258700">Aww so lovely!! Beautiful capture.</comment>
    <comment id="49862655-5692627867-72157626662413410" author="61373986@N06" authorname="My NIKON And Me" iconserver="5310" iconfarm="6" datecreate="1304716098" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626662413410">lovely image!!!</comment>
    <comment id="49862655-5692627867-72157626663408864" author="7652657@N02" authorname="Majlee" iconserver="3130" iconfarm="4" datecreate="1304728344" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663408864">This is just adorable !</comment>
    <comment id="49862655-5692627867-72157626663519092" author="15613254@N05" authorname="mr_jyoti" iconserver="4011" iconfarm="5" datecreate="1304729940" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663519092">Cool shot. Nice bokey.</comment>
    <comment id="49862655-5692627867-72157626663642456" author="16327396@N03" authorname="my beanie hat rocks" iconserver="2550" iconfarm="3" datecreate="1304731810" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663642456">Maybe she could cheer this fella up!!

&lt;a href=&quot;http://www.flickr.com/photos/weasteman/5652855802/in/photostream&quot;&gt;www.flickr.com/photos/weasteman/5652855802/in/photostream&lt;/a&gt;

=D</comment>

I'm writing an iPhone app that downloads data from the Flickr API. At the moment there seems to be no way to limit how many comments it downloads, and although I'd like to get maybe 8 or 10, it sometimes sends me hundreds. I have subclassed ASIHTTPRequest so that it will only download a set amount of bytes (for example, it will stop downloading after receiving 1024 bytes of comment data).

Now, the information I'd like to parse is all there (the comment data contains things like a user id, the text, etc.). However, since it is cut off before the end, the XML is malformed, and my current solution (using ObjectiveFlickr's XML parser) is unable to parse the XML. Is there a way to handle badly formed XML, a la the way old web browsers handled HTML, and only extract the data that is well-formed?

Here is some sample data:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<comments photo_id="5692627867">
    <comment id="49862655-5692627867-72157626659891768" author="29114051@N05" authorname="eαse*" iconserver="4046" iconfarm="5" datecreate="1304689286" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626659891768">lovely lovely lovelyyyyy!!! ♥♥♥♥♥♥♥♥♥</comment>
    <comment id="49862655-5692627867-72157626535581359" author="49946698@N06" authorname="RandomPics Art" iconserver="4017" iconfarm="5" datecreate="1304692593" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626535581359">nice! like soft colors and tones!</comment>
    <comment id="49862655-5692627867-72157626660240896" author="49907977@N06" authorname="kiki_chi" iconserver="4014" iconfarm="5" datecreate="1304693051" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660240896">&gt;eαse*
&gt;RandomPics Art

Thank you:) :) :)</comment>
    <comment id="49862655-5692627867-72157626660761230" author="41717031@N08" authorname="petia.bourova" iconserver="4082" iconfarm="5" datecreate="1304698244" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626660761230">Thank you!Very nice photo!I like The coulers very,very much!!!</comment>
    <comment id="49862655-5692627867-72157626661258700" author="31540474@N08" authorname="Leentje32" iconserver="4067" iconfarm="5" datecreate="1304703576" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626661258700">Aww so lovely!! Beautiful capture.</comment>
    <comment id="49862655-5692627867-72157626662413410" author="61373986@N06" authorname="My NIKON And Me" iconserver="5310" iconfarm="6" datecreate="1304716098" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626662413410">lovely image!!!</comment>
    <comment id="49862655-5692627867-72157626663408864" author="7652657@N02" authorname="Majlee" iconserver="3130" iconfarm="4" datecreate="1304728344" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663408864">This is just adorable !</comment>
    <comment id="49862655-5692627867-72157626663519092" author="15613254@N05" authorname="mr_jyoti" iconserver="4011" iconfarm="5" datecreate="1304729940" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663519092">Cool shot. Nice bokey.</comment>
    <comment id="49862655-5692627867-72157626663642456" author="16327396@N03" authorname="my beanie hat rocks" iconserver="2550" iconfarm="3" datecreate="1304731810" permalink="http://www.flickr.com/photos/kikicchi/5692627867/#comment72157626663642456">Maybe she could cheer this fella up!!

<a href="http://www.flickr.com/photos/weasteman/5652855802/in/photostream">www.flickr.com/photos/weasteman/5652855802/in/photostream</a>

=D</comment>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-11-13 08:52:20

我不确定确切的 XML 格式,但它看起来很简单。在这种情况下,您可以尝试找出数据中的最后一个标签并手动添加缺少的结束标签。它不应该只是一个简单的字符串搜索和搜索。代替。

I am not sure about the exact XML format, but it looks simple. In such case you can try to figure out last tag in the data and add the missing closing tags manually. It should not be more than a simple string search & replace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文