3

我必须解析相对大量的 XML 数据,因此我打算使用TBXMLParser. 我对它很陌生。下面是示例 XML 格式。

<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”&gt;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”&gt;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”&gt;Google-Png</media:title>
</media:content>
</item>


and it goes on...

我想获得第二个 url ='Have to...' 的值。

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

这是代码,我试过了。使用它,我能够获得第一个url属性值。任何建议将不胜感激。谢谢 :)

4

2 回答 2

0
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];

        while (element != nil) {
TBXMLElement *iconElement = [TBXML childElementNamed:@"Image" parentElement:element];
strIcon = [TBXML valueOfAttributeNamed:@"url" forElement:iconElement];

    }
}

希望这行得通??

于 2012-09-24T12:49:58.197 回答
0

根据您编辑的示例数据获取第二个节点的值..

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]);

}
于 2012-09-25T09:12:35.997 回答