【问题标题】:XMLDictionary not parsing complete XML dataXMLDictionary 不解析完整的 XML 数据
【发布时间】:2014-01-16 17:58:28
【问题描述】:

我正在使用https://github.com/nicklockwood/XMLDictionary 类别将服务器 XML 响应转换为 NSDictionary。它似乎只工作了一半,我不确定这是否是我使用它的方式有问题,或者 XMLDictionary 中是否存在错误或什么。

我正在调用这样的方法: NSDictionary *convertedData = [NSDictionary dictionaryWithXMLData:data];

这是我要解析的 XML 数据示例(这直接来自我传递的“数据”):

<?xml version="1.0" encoding="utf-8"?>
<anime>
  <entry>
    <id>572</id>
    <title>Kaze no Tani no Nausicaa</title>
    <english>Nausica&auml; of the Valley of the Wind</english>
    <synonyms>Nausicaa of the Valley of the Wind; Warriors of the Wind</synonyms>
    <episodes>1</episodes>
    <score>8.47</score>
    <type>Movie</type>
    <status>Finished Airing</status>
    <start_date>1984-03-11</start_date>
    <end_date>1984-03-11</end_date>
    <synopsis>A thousand years after a global war, a seaside kingdom known as the Valley Of The Wind remains one of only a few areas still populated. Led by the courageous Princess Nausica&auml;, the people of the Valley are engaged in a constant struggle with powerful insects called ohmu, who guard a poisonous jungle that is spreading across the Earth. Nausica&auml; and her brave companions, together with the people of the Valley, strive to restore the bond between humanity and the Earth. &lt;br /&gt;
&lt;br /&gt;
(Source: Disney)</synopsis>
    <image>http://cdn.myanimelist.net/images/anime/7/42071.jpg</image>
  </entry>
</anime>

这是我得到的结果:

convertedData的打印说明:

{
    "__name" = anime;
    entry =     {
        english =         {
        };
        id = 572;
        title = "Kaze no Tani no Nausicaa";
    };
}

问题是,并非所有搜索结果都会出现这种情况。使用相同的搜索结果完全可以重复,但在某些结果中它根本不这样做。我不知道这些结果有什么特别之处,使它在其他人没有的地方起作用。这是一个有效的例子。

XML 输入:

<?xml version="1.0" encoding="utf-8"?>
<anime>
  <entry>
    <id>329</id>
    <title>Planetes</title>
    <english></english>
    <synonyms></synonyms>
    <episodes>26</episodes>
    <score>8.42</score>
    <type>TV</type>
    <status>Finished Airing</status>
    <start_date>2003-10-04</start_date>
    <end_date>2004-04-17</end_date>
    <synopsis>In the year 2075, mankind has reached a point where journeying between Earth, the moon and the space stations is part of daily life. However, the progression of technology in space has also resulted in the problem of the space debris, which can cause excessive and even catastrophic damage to spacecrafts and equipment. This is the story of Technora&amp;#039;s Debris Collecting section, its EVA worker, Hachirota &amp;quot;Hachimaki&amp;quot; Hoshino, and the newcomer to the group, Ai Tanabe. &lt;br /&gt;
&lt;br /&gt;
(Source: ANN)</synopsis>
    <image>http://cdn.myanimelist.net/images/anime/8/50463.jpg</image>
  </entry>
  <entry>
    <id>10735</id>
    <title>Planetes Picture Drama</title>
    <english></english>
    <synonyms>Planetes Audio Drama</synonyms>
    <episodes>9</episodes>
    <score>6.27</score>
    <type>Special</type>
    <status>Finished Airing</status>
    <start_date>2009-09-25</start_date>
    <end_date>2009-09-25</end_date>
    <synopsis>Planetes picture drama.</synopsis>
    <image>http://cdn.myanimelist.net/images/anime/9/29571.jpg</image>
  </entry>
</anime>

NSDictionary 输出:

{
    "__name" = anime;
    entry =     (
                {
            "end_date" = "2004-04-17";
            episodes = 26;
            id = 329;
            image = "http://cdn.myanimelist.net/images/anime/8/50463.jpg";
            score = "8.42";
            "start_date" = "2003-10-04";
            status = "Finished Airing";
            synopsis = "In the year 2075, mankind has reached a point where journeying between Earth, the moon and the space stations is part of daily life. However, the progression of technology in space has also resulted in the problem of the space debris, which can cause excessive and even catastrophic damage to spacecrafts and equipment. This is the story of Technora&#039;s Debris Collecting section, its EVA worker, Hachirota &quot;Hachimaki&quot; Hoshino, and the newcomer to the group, Ai Tanabe. <br />\n<br />\n(Source: ANN)";
            title = Planetes;
            type = TV;
        },
                {
            "end_date" = "2009-09-25";
            episodes = 9;
            id = 10735;
            image = "http://cdn.myanimelist.net/images/anime/9/29571.jpg";
            score = "6.27";
            "start_date" = "2009-09-25";
            status = "Finished Airing";
            synonyms = "Planetes Audio Drama";
            synopsis = "Planetes picture drama.";
            title = "Planetes Picture Drama";
            type = Special;
        }
    );
}

有时它只会省略某些标签,例如标签。其他时候,它会像我的第一个示例中那样省略大部分内容。我真的很困惑。

另外,如果我向它传递一个大的 XML 文件(比如一个包含 50 个项目的搜索结果),它只会解析其中的一半。我怀疑有人想通读所有内容,但以防万一,这里是 XML http://pastebin.com/aQTc8S9x 和这里是 http://pastebin.com/MwuexaZV 出来的 NSDictionary

【问题讨论】:

    标签: ios objective-c xml xml-parsing nsdictionary


    【解决方案1】:

    看起来它被 &amp;auml; 和其他 html 实体绊倒了。在传递给解析器之前,首先在 XML 字符串上处理它们。 Here 是另一个关于如何做到这一点的 SO 问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2011-04-22
    • 2012-10-28
    • 2012-04-08
    • 1970-01-01
    • 2014-09-25
    相关资源
    最近更新 更多