如何使用NSXMLParser解析基本XML文件? [英] How do I parse a basic XML file with NSXMLParser?
问题描述
我试图找出我的iPhone应用程序的NSXMLParser,虽然我一般理解它的工作原理,我仍然有点困惑,如何提取我需要的值。
I am trying to figure out NSXMLParser for my iPhone app and while I generally understand how it works, I am still a little confused about how to extract the values I need.
我解析的XML结果是非常基本的。它是这样:
The XML result that I am parsing is very basic. it is like so:
<start>
<status>300</status>
<record>
<title>The Title</title>
<content>Some content</content>
</record>
</start>
我需要做3件事情:
获取status的值。
从第一条记录获取内容的值。可能会有一个响应提供多个记录元素,所以我需要只得到第一个。
I need to do 3 things: Get the value of status. Get the value of content from the first record. There may come a response that offers multiple "record" elements so I need to only get the first.
我无法弄清楚如何简单地这样做。我看到的大多数所有的例子涉及创建一个单独的对象来填充这些数据,我不能看到是必要的2值。任何人都可以告诉我如何将这两个数据拉出来,并且只有第一条记录?
I can't figure out how to simply do that. Most all of the examples I have seen involve creating a separate object to populate this data into and I can't see that being necessary for 2 values. Can anyone tell me how to pull these 2 pieces of data out and only for the first record?
推荐答案
当 NSXMLParser
遇到XML标签时,将调用委托方法 parser:didStartElement:namespaceURI:qualifiedName:attributes:
;你可能只需要在这里使用 elementName
变量。然后,XML解析器读取标记中的字符,并与内容一起调用 parser:foundCharacters:
。最后 parser:didEndElement:namespaceURI:qualifiedName
被调用。
The first thing that happens when the NSXMLParser
encounters an XML tag is that the delegate method parser:didStartElement:namespaceURI:qualifiedName:attributes:
is called; you'll probably only need to use the elementName
variable here. Then, the XML parser reads the characters in the tag and calls parser:foundCharacters:
with the contents. Finally parser:didEndElement:namespaceURI:qualifiedName
is called.
我采取的方法,在 SeismicXML 考试中使用以下方法:
The approach that I've taken, as Apple uses in the SeismicXML examlple, is to use the methods as follows:
- 在
:didStartElement:namespaceURI:qualifiedName:attributes:
,将元素名称的字符串与已知值进行比较,看看它是否是您关心的字符串。如果是这样,然后设置一个实例变量(NSMutableString
;我将其称为contentOfCurrentXMLProperty
)为空字符串。否则将其设置为nil
。 - 在
parser:foundCharacters:
contentOfCurrentXMLProperty
找到的字符。 - 在
解析器:didEndElement:namespaceURI:qualifiedName
,将contentofCurrentXMLProperty
的值赋给任何适当的变量。
- In
parser:didStartElement:namespaceURI:qualifiedName:attributes:
, compare the string of the element name to a known value to see if it's a string you care about. If so, then set an instance variable (anNSMutableString
; I'll call itcontentOfCurrentXMLProperty
) to an empty string. Otherwise set it tonil
. - In
parser:foundCharacters:
, append the found characters tocontentOfCurrentXMLProperty
. - In
parser:didEndElement:namespaceURI:qualifiedName
, assign the value ofcontentofCurrentXMLProperty
to whatever the appropriate variable is.
有关详细信息,请参阅 SeismicXML 示例。
See the SeismicXML example for more information.
有关您的特定情况的几个问题:首先,由于XML解析器只返回字符串需要将 status
的字符串转换为整数(或您使用的任何数据类型)。
A couple of things about your specific case: first, since the XML parser only returns strings, you'll need to convert the string to an integer (or whatever data type you're using) for status
.
第二,因为你只需要记录
的第一个值,在 parser:didStartElement:...
设置 BOOL
,用于标记您之前是否已经看到记录
标记,如果是,则设置 contentOfCurrentXMLProperty
至 nil
。
Second, since you only want the first value for record
, in parser:didStartElement:...
I'd set up a BOOL
that flags whether you've already seen a record
tag before and, if so, set contentOfCurrentXMLProperty
to nil
.
这篇关于如何使用NSXMLParser解析基本XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!