无法使用 TouchXML 解析和构建嵌套 XML 元素
当嵌套元素出现在 xml 中时如何使用 TouchXML?我希望需要将图像名称嵌套在 NSArray 中作为字典键 @"images",但是代码有问题:(。这是 XML 的结构:
<itemList>
<item>
<name>SomeName</name>
<description>Some description</description>
<images>
<image>image1.png</image>
<image>image5.png</image>
</images>
</item>
<item>
<name>SomeName</name>
<description>Some description</description>
<images>
<image>image1.png</image>
<image>image5.png</image>
</images>
</item>
</itemList>
这是解析代码:
resultNodes = [itemListParser nodesForXPath:@"//items" error:nil];
NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];
itemList = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *images = [[NSMutableArray alloc] init];
// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
for(int i = 0; i < [resultElement childCount]; i++) {
if ([[[resultElement childAtIndex:i] name] isEqualToString: @"images"]){
[images removeAllObjects];
for(int j = 0; j < [[resultElement childAtIndex:i] childCount]; j++) {
[images addObject:[[resultElement childAtIndex:j] stringValue]];
}
[itemDic setObject:images forKey:[[resultElement childAtIndex:i] name]];
} else {
[itemDic setObject:[[resultElement childAtIndex:i] stringValue] forKey:[[resultElement childAtIndex:i] name]];
}
}
[itemList addObject:[[itemDic copy] autorelease]];
}
[itemDic release];
[images release];
How to use TouchXML when nested elements appear inside xml? I would like need to nest image names inside NSArray for dictionary key @"images", however something is wrong with the code :(. Here's the structure of XML:
<itemList>
<item>
<name>SomeName</name>
<description>Some description</description>
<images>
<image>image1.png</image>
<image>image5.png</image>
</images>
</item>
<item>
<name>SomeName</name>
<description>Some description</description>
<images>
<image>image1.png</image>
<image>image5.png</image>
</images>
</item>
</itemList>
Here's the parsing code:
resultNodes = [itemListParser nodesForXPath:@"//items" error:nil];
NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];
itemList = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *images = [[NSMutableArray alloc] init];
// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {
for(int i = 0; i < [resultElement childCount]; i++) {
if ([[[resultElement childAtIndex:i] name] isEqualToString: @"images"]){
[images removeAllObjects];
for(int j = 0; j < [[resultElement childAtIndex:i] childCount]; j++) {
[images addObject:[[resultElement childAtIndex:j] stringValue]];
}
[itemDic setObject:images forKey:[[resultElement childAtIndex:i] name]];
} else {
[itemDic setObject:[[resultElement childAtIndex:i] stringValue] forKey:[[resultElement childAtIndex:i] name]];
}
}
[itemList addObject:[[itemDic copy] autorelease]];
}
[itemDic release];
[images release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该手动迭代节点。使用 XPath 总是更好。查看 TouchXML xpath API。
You shouldn't iterate over your nodes manually. It is always almost better to use XPath. Look at the TouchXML xpath APIs.
resultNodes = [itemListParsernodesForXPath:@"//item" 错误:nil];
不是物品
resultNodes = [itemListParser nodesForXPath:@"//item" error:nil];
not items