Icoder
Icoder

Reputation: 330

Getting attributes of Child and Subchild using NSXmlParser

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

Upvotes: 1

Views: 1066

Answers (2)

Anil Kothari
Anil Kothari

Reputation: 7733

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

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>

Initialize topicArray and subTopicArray (NSMutableArrays) in viewDidLoad method. take BOOL searchDone in header file.

To parse the 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);

        }

Upvotes: 1

Chris Parker
Chris Parker

Reputation: 1252

If you're talking about getting a value for the "Name" key of each XML tag, those are handed to you in the 'attributeDict' parameter of the parser:didStartElement:namespaceURI:qualifiedName:attributes: delegate method...

Upvotes: 0

Related Questions