无法使用 TouchXML 解析和构建嵌套 XML 元素

发布于 2024-12-11 11:07:04 字数 1791 浏览 0 评论 0原文

当嵌套元素出现在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-12-18 11:07:04

您不应该手动迭代节点。使用 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.

终陌 2024-12-18 11:07:04

resultNodes = [itemListParsernodesForXPath:@"//item" 错误:nil];

不是物品

resultNodes = [itemListParser nodesForXPath:@"//item" error:nil];

not items

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文