[英]Parsing XML with NSXMLParser and getting specific property value
UPDATE: I decided to change my web service to output JSON, much easier to work with. 更新:我决定将我的Web服务更改为输出JSON,更易于使用。 I'll leave this open incase anyone can come up with a sensible solution, and in case it helps others.
如果有人可以提出一个明智的解决方案,并且可以帮助其他人,我将保持开放态度。
I'm grabbing an XML file, parsing it, and trying to get content from specific tags: 我正在抓取一个XML文件,对其进行解析,然后尝试从特定标签中获取内容:
//Grab the hosted XML
NSError *error;
NSURL *mainContentURL = [NSURL URLWithString:@"http://www.andrewlarking.co.uk/DigiCons/appContent.xml"];
NSString *mainPageContents = [NSString stringWithContentsOfURL:mainContentURL encoding:NSUTF8StringEncoding error:&error];
NSData *mainPageData = [mainPageContents dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"mainPageData returned = %@", [[NSString alloc] initWithData:mainPageData encoding:NSUTF8StringEncoding]);
//Parse the XML
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:mainPageData];
[parser setDelegate:self];
[parser parse];
}
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"In parser");
if([elementName isEqualToString:@"feb5Content"])
{
NSLog(@"In elementname");
NSLog(@"valueforkey title = %@", [attributeDict valueForKey:@"page.title"]);
self.dcViewControllerLabel.text = [attributeDict valueForKey:@"page.title"];
}
}
All is good, but the value for key@"page.title" or @"title" returns null. 一切都很好,但是key @“ page.title”或@“ title”的值返回null。 Here is the XML:
这是XML:
<feb5Content>
<page>
<title>I am a test title.</title>
<subTitle>I am the subtitle.</subTitle>
</page>
</feb5Content>
Any thoughts? 有什么想法吗?
Ideally I'd like to be able to grab the content of various elements that exist within the element. 理想情况下,我希望能够获取元素中存在的各种元素的内容。 So I could have title, subTitle, bodyText all under and I'd need to grab those.
因此,我可以将title,subTitle,bodyText全部包含在下面,而我需要抓住它们。 Each page of the app will have it's contents stored in a element.
应用程序的每个页面都会将其内容存储在一个元素中。
Cheers. 干杯。
That's because your page title is not an attribute of the <feb5Content>
element. 那是因为您的页面标题不是
<feb5Content>
元素的属性。 It is the value of the <title>
element. 它是
<title>
元素的值。 So you have to listen to the start of the <title>
element: 因此,您必须聆听
<title>
元素的开头:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
self.isTitleElement = [elementName isEqualToString:@"title"];
}
And then in the following method get the value inside the <title>
element: 然后在以下方法中获取
<title>
元素内的值:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (self.isTitleElement) {
NSLog(@"valueforkey title = %@", string);
// store value so that it is not overwritten by the next title
[self.pageTitles addObject:string];
}
}
This method is called everytime that text is found inside an XML element. 每当在XML元素中找到文本时,都会调用此方法。 So you have to check if you are inside the
<title>
element when this method is called. 因此,在调用此方法时,必须检查是否在
<title>
元素内。 That's why I included the ivar self.isTitleElement
. 这就是为什么我包含ivar
self.isTitleElement
的原因。
To store the page titles you can put them into an NSMutableArray ivar. 要存储页面标题,可以将其放入NSMutableArray ivar中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.