解析iOS中的XML结束标记(目标C)?

时间:2016-04-12 06:21:59

标签: ios objective-c xml xml-parsing

我想从我的xml响应中识别结束标记。例如,

<ItemLocationResponse xxxx>
    <UserProfile>
        blah blah contents..
    </UserProfile>
    <UserProfile>
        blah blah contents..
    </UserProfile>
</ItemLocationResponse>

我想识别结束标记。目前我能够解析包含在开始标记中的数据。从上面的例子中,我可以从每个开始标签解析包含“blah blah contents”的数据。我尝试了很多用于获取结束标记以确定xml响应已经结束。那么我怎样才能确定结束标签已经过来了?

2 个答案:

答案 0 :(得分:1)

行动简单,使用XMLDictionary

答案 1 :(得分:1)

您可以在iOS中使用NSXMLParser进行xml解析。

NSXMLParser * parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];

实现其委托方法以获取xml的元素。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
//Called when any element which having opening and closing element and its closed called

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
//if tag having value then this method is called.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError