Search code examples
iphonetbxml

Attribute value using TBXMLParser


I have to parse relatively huge amount of XML data, thus I am going for TBXMLParser. And I am pretty new to it. Below is the sample XML format.

<item>
<title> Test XML</title>
<link>http://www.google.com</link>
<media:content url="http://blog.directorymaximizer.com/wp-content/uploads/2012/09/google.jpg" medium="image">
    <media:title type="html”>Google</media:title>
</media:content>

<media:content url="http://icons.iconarchive.com/icons/fasticon/web-2/256/Google-icon.png" medium="image">
    <media:title type="html”>Google-Png</media:title>
</media:content>

<media:content url="http://icons.iconarchive.com/icons/fasticon/web-2/256/Google-icon.png" medium="image">
<media:title type="html”>Google-Png</media:title>
</media:content>
</item>


and it goes on...

I want to get the value of second url =‘Have to...’.

TBXMLElement *iconElement = [TBXML childElementNamed:@"Image" parentElement:element];
strIcon = [TBXML valueOfAttributeNamed:@"url" forElement:iconElement];

This is the code, I tried. Using this, I was able to get the first url attribute value. Any suggestions would be highly appreciated. Thank you :)


Solution

  • To get the value of second Node.. based on your edited sample data..

    TBXML *tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:yourXMLString]];  //replace yuorXMLString with your string
    TBXMLElement * root = tbxml.rootXMLElement;
    
    // if root element is valid
    
    
    if (root != nil) {
        TBXMLElement * element = [TBXML childElementNamed:@"item" parentElement:root];
    
             TBXMLElement *node= [TBXML childElementNamed:@"media:content" parentElement:element];
              TBXMLElement *secondNode= [TBXML nextSiblingNamed:@"media:content" searchFromElement:node];
            NSLog(@"the second url name is : %@",[TBXML valueOfAttributeNamed:@"url" forElement:secondNode]);
    
    }