使用NSXmlParser获取Child和Subchild的属性 [英] Getting attributes of Child and Subchild using NSXmlParser
问题描述
这是我的XML结构....
This is my XML structure....
<course>
<Topic Name="Child1">
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
</Topic>
<Topic Name="Child2">
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
</Topic>
<Topic Name="Child3">
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
</Topic>
</course>
现在我正在使用这三个代表
Right now I'm using these three Delegates
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
我可以通过这些代表获取节点内的数据...... ..但我不知道如何获得子和子孩子的属性
I can get the data inside the node by means of these delegates..... But I don't know how to get the attributes of child and sub childs
推荐答案
首先你的xml是不正确的它应该是这样的。我在这个链接上查看了
first of all your xml is incorrect it should be like this.I have check it on this link
http://www.w3schools.com/xml/xml_validator.asp
子主题不存在关闭标记。 XML应该是这样的。
The close tag doesn't exist for the subtopic. The XML should be like this.
<Topic Name="Child2">
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
</Topic>>
<Topic Name="Child3">
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
<subtopic Name="Subchild"> </subtopic>
</Topic>
初始化topicArray和subTopicArray(NSMutableArrays )在viewDidLoad方法中。
在头文件中使用BOOL searchDone。
Initialize topicArray and subTopicArray (NSMutableArrays) in viewDidLoad method. take BOOL searchDone in header file.
解析xml: -
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString: @"Topic"] ){
if ([attributeDict objectForKey:@"Name"] isEqualToString:@"Child2"); {
searchDone=YES;
}
else {
searchDone=NO;
}
}
if ([elementName isEqualToString: @"subtopic"]){
if (searchDone==YES)
{
[subTopicArray addObject:[attributeDict objectForKey:@"Name"]];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"Child2 subtopic data %@",subTopicArray);
}
这篇关于使用NSXmlParser获取Child和Subchild的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!