我必须解析相对大量的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”>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...我想要获取第二个url =‘必须...’的值。
TBXMLElement *iconElement = [TBXML childElementNamed:@"Image" parentElement:element];
strIcon = [TBXML valueOfAttributeNamed:@"url" forElement:iconElement];这是代码,我试过了。使用它,我能够获得第一个url属性值。任何建议都将不胜感激。谢谢您:)
发布于 2012-09-25 17:12:36
获取第二个节点的值..根据您编辑的样本数据..
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-24 20:49:58
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];
}
}希望这能起作用??
https://stackoverflow.com/questions/12564978
复制相似问题