Objective c iPhone中的xml解析和获取其他同名标记

Objective c iPhone中的xml解析和获取其他同名标记,objective-c,xml,parsing,Objective C,Xml,Parsing,让我尽可能清楚地解释这个问题的确切含义 xml看起来像这样 <Books> <Book id="1"> <title>Circumference</title> <author>Nicholas Nicastro</author> <summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary> </Book&

让我尽可能清楚地解释这个问题的确切含义

xml看起来像这样

<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary>How the scientific revolution began</summary>

</Book>

</Books>
<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary id ='1'>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary id ='1'>How the scientific revolution began</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

</Books>

周长
尼古拉斯·尼卡斯特罗
埃拉托斯提尼和古代测量地球的探索。
哥白尼秘密
杰克·瑞切克
科学革命是如何开始的
看起来像这样

<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary>How the scientific revolution began</summary>

</Book>

</Books>
<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary id ='1'>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary id ='1'>How the scientific revolution began</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

</Books>

周长
尼古拉斯·尼卡斯特罗
埃拉托斯提尼和古代测量地球的探索。
Eratosthenes在另一个标签中提供更多信息。
埃拉托什尼和另一个标签。
埃拉托什内斯和最后一个标签另一个在这里
哥白尼秘密
杰克·瑞切克
科学革命是如何开始的
Eratosthenes在另一个标签中提供更多信息。
埃拉托什尼和另一个标签。
埃拉托什内斯和最后一个标签另一个在这里
现在,如果我按照上面列出的网站上的说明进行操作,它不会解释如何处理摘要2,3,4(我需要解析的xml看起来是这样的)以及如何显示它们的输出。我只会得到最后一行。有没有人知道我怎样才能得到其他的值(在本例中是2,3,它似乎只显示最后一个,因为这可能是currentElementValue中的最后一个)


我有点困惑,我应该在这里处理这个属性,还是应该在解析器中创建一个新的搜索标记

您需要跟踪到目前为止已解析的摘要元素,方法是将所有值保存在某个容器中,如array:NSMutableArray。因此,您可以使用NSMutableArray来保存已解析的摘要列表,而不是使用NSString来保存摘要

无论何时在解析器中遇到摘要,都不会将NSString摘要设置为刚读取的字符串(这将替换旧值并解释为什么只获取最后一个摘要标记)。而是将其存储为新字符串,并将该字符串添加到NSMutableArray中

您链接到的博客文章中的设计存在的问题是,它使用keyValueCoding来设置Book对象中的属性,这不利于将项目添加到数组项目中。因此,您将需要在解析器中包含对summary元素的一些特殊处理,并向Book类中添加允许您向summary数组添加项的方法。请参见如何使用KVC实现这一点。

我认为这是您需要了解的,您可以从属性中获取id字段的值,并使用该值将其分配给一个变量,然后使用该变量

因此,我的didStartElement中可能有这样的内容(其中属性是在标题中声明的变量):

然后在我的foundCharacters中有类似的内容:

if([[attributes valueForKey:@"id"] intValue] == 1){
    doSomething
}else if([[attributes valueForKey:@"id"] intValue] == 2){
    doSomethingElse
}...
等等,直到你把所有的数据都拿出来


注意:这是100%未经测试的代码,但我相信它可能会工作…

thnx很多。。我要调查这件事。。。它看起来很有希望,不幸的是,我必须阅读关键的编码文档,以便更好地理解;)。thnx很多,尽管我不得不说,这感觉有点mmm变通方法,比如,我更喜欢一种更有效的方法,尽管我非常欣赏你的意见,如果另一种方法失败了,我甚至可能会使用它。