使用NSXmlParser获取Child和Subchild的属性

时间:2011-12-08 05:42:23

标签: iphone nsxmlparser

这是我的XML结构......

<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>

现在我正在使用这三个代表

- (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 
{
}

我可以通过这些代理获取节点内的数据.....但我不知道如何获取子节点和子节点的属性

2 个答案:

答案 0 :(得分:1)

首先你的xml不正确它应该是这样的。我在这个链接上检查了它

http://www.w3schools.com/xml/xml_validator.asp

子主题不存在关闭标记。 XML应该是这样的。

                                                           

<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>

在viewDidLoad方法中初始化topicArray和subTopicArray(NSMutableArrays)。 在头文件中获取BOOL searchDone。

解析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);

        }

答案 1 :(得分:0)

如果您正在讨论为每个XML标记的“名称”键获取值,那么这些值将在parser:didStartElement:namespaceURI:qualifiedName:attributes:委托方法的'attributeDict'参数中传递给您...