如何使用NSXMLParser解析XML [英] How to parse XML with NSXMLParser

查看:127
本文介绍了如何使用NSXMLParser解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<Message>
    <Text>Town park travel</Text>
    <Theme>Ready for racing?</Theme>
    <Activate_date>2011-03-08</Activate_date>
    <Login>Simon</Login>
    <Name>Simon </Name>
    <Longitude>46.00339</Longitude>
    <Latitude>51.515381</Latitude>
</Message>

如何获取元素到NSMutableDictionary?

How can I get elements To NSMutableDictionary?

i try

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
if ([elementName isEqualToString:@"Message"]){
    listOfFriends = [[NSMutableDictionary alloc] init];
    if ([elementName isEqualToString:@"text"]) {
        [listOfFriends setObject: elementName forKey @"text" ];
    }
    }

但是错了

现在我使用相同的代码:

Now i use same code:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
if ([elementName isEqualToString:@"Message"]){
    listOfFriends = [[NSMutableDictionary alloc] init];
} else if ([elementName isEqualToString:@"Text"] || [elementName isEqualToString:@"Theme"] || [elementName isEqualToString:@"Activate_date"] || [elementName isEqualToString:@"Login"] || [elementName isEqualToString:@"Longitude"] || [elementName isEqualToString:@"Latitude"]) {
    [currentElement setString:@""];
    storingCharacters = YES;
}
}   

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"Message"]) {
    [listOfFriends release];
} else if ([elementName isEqualToString:@"Text"]) {
    txt.text = currentElement;
} else if ([elementName isEqualToString:@"Theme"]) {
    theme.text = currentElement;
} else if ([elementName isEqualToString:@"Activate_date"]) {
    aDate.text = currentElement;
} else if ([elementName isEqualToString:@"Login"]) {
    autor.text = currentElement;
} 
storingCharacters = NO;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (storingCharacters) [currentElement appendString:string];
}

但我有空的Lables视图...

But i have empty Lables on view...

推荐答案

NSXMLParser是基于流的,当遇到XML流中的特定组件时,向其委托发送各种消息 - 开始标签,文本,结束标签,等等。因此,创建NSXMLParser的实例,并为其分配您所编写的符合NSXMLParserDelegate 协议。 (编辑:从您添加到您的问题的代码中,您似乎可能已经完成了这一部分,或者至少做了一个好的开始。)

NSXMLParser is stream-based, and sends various messages to its delegate when it encounters particular components in the XML stream - an opening tag, text, an end tag, and so forth. So, create an instance of NSXMLParser, and assign to it a delegate that you write that conforms to the NSXMLParserDelegate protocol. ( From the code you added to your question, it looks like you may have already done this part, or at least made a good start on it.)

在您的委托类' -parser:didStartElement:... 方法,请检查元素名称。如果它是消息,创建一个新的NSMutableDictionary来保存其子元素中的信息,并将其存储在属性中:(编辑:在您的代码中,它看起来像你已声明实例变量,但你应该

In your delegate class' -parser:didStartElement:... method, check the element name. If it's "Message", create a new NSMutableDictionary to hold the information in its child elements, and store that in a property: ( In your code, it looks like you've declared the instance variable, but you should use dot-syntax to access it as a property, to make sure the correct memory-management happens.)

self.listOfFriends = [NSMutableDictionary dictionary];

对于其他元素名称,创建一个空的NSMutableString,用于保存文本, a property:

For other element names, create an empty NSMutableString that will be used to hold text, and store that in a property:

self.thisString = [NSMutableString string];

在您的 -parser:foundCharacters: 方法,将字符串附加到您之前创建的可变字符串属性。

In your -parser:foundCharacters: method, append the string to the mutable string property you created earlier.

[self.thisString appendString:string];

最后,在 -parser:didEndElement :. .. 方法,检查元素名称。如果它是消息,你完成了这个消息元素。如果您的XML流只有一个Message元素(如示例所示),您现在可以打印您构建的字典,或者您想要使用的任何其他字符。如果可以有多个Message元素,则可以将刚刚完成的元素添加到数组。

Finally, in your -parser:didEndElement:... method, check the element name. If it's "Message," you're done with this message element. If your XML stream has only one Message element (as in the example), you can now print the dictionary you've built, or whatever else it is you wanted to do with it. If there can be multiple Message elements, you could add the just-completed one to an array.

如果刚刚结束的元素不是 a Message,将其添加到字典中 -setValue:forKey: 将元素名称作为键传递,您在创建之前创建的 -parser:foundCharacters:消息作为值。

If the just-ended element is not a Message, add it to the dictionary with -setValue:forKey: passing the element name as a key, and the built-up string you created while receiving earlier -parser:foundCharacters: messages as the value.

[self.listOfFriends setValue:self.thisString forKey:elementName];

这篇关于如何使用NSXMLParser解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆