【问题标题】:How to parse child node value of an xml through kissxml parser?如何通过kissxml解析器解析xml的子节点值?
【发布时间】:2011-08-02 20:39:24
【问题描述】:

我有这个 xml 文件:

<Menu>
<item value="boiled">
    <image value="boiling1recipe">boiling1.jpg</image>
    <recipeImage>png1.png</recipeImage>
    <recipeImage>png2.png</recipeImage>
    <recipeImage>png3.png</recipeImage>
    <recipeImage>png4.png</recipeImage>
  </item>
  <item value="boiled">
    <image value="boiling2recipe">boiling2.jpeg</image>
    <recipeImage>png5.png</recipeImage>
    <recipeImage>png6.png</recipeImage>
    <recipeImage>png7.png</recipeImage>
    <recipeImage>png8.png</recipeImage>
  </item>
  <item value="rosted">
    <image value="roasted1recipe">roasted1.jpeg</image>
    <recipeImage>png8.png</recipeImage>
    <recipeImage>png9.png</recipeImage>
    <recipeImage>png10.png</recipeImage>
    <recipeImage>png11.png</recipeImage>
   </item>
  <item value="rosted">
    <image value="roasted2recipe">roasted2.jpeg</image>
    <recipeImage>png12.png</recipeImage>
    <recipeImage>png13.png</recipeImage>
    <recipeImage>png1.png</recipeImage>
    <recipeImage>png2.png</recipeImage>
   </item>
</Menu>
现在, 我是这样解析的:
    DDXMLNode *node = [item attributeForName:@"value"];

    if ([[node stringValue] isEqual:@"boiled"]) {
        [listOfBoiledItems addObject:model];

        DDXMLNode *node1 = [item attributeForName:@"value"];

        if ([[node1 stringValue] isEqual:@"boiling1recipe"]) {
            [listOfRecipies1 addObject:model];
        }
        else if ([[node1 stringValue] isEqual:@"boiling2recipe"]) { 
            [listOfRecipies2 addObject:model];
        }
     }
            else if ([[node stringValue] isEqual:@"rosted"]) {  
        [listOfRostedItems addObject:model];

        DDXMLNode *node1 = [item attributeForName:@"value"];

        if ([[node1 stringValue] isEqual:@"roasted1recipe"]) {  
            [listOfRecipies6 addObject:model];
        }
        else if ([[node1 stringValue] isEqual:@"roasted2recipe"]) {
            [listOfRecipies7 addObject:model];
        }
    }

这里, 编译器读取父节点值,即“boiled”和“rosted”,但不读取智利节点值,即“boiling1recipe”、“boiling2recipe”、“roasted1recipe”和“roasted2recipe”。 可能是什么问题,意味着我做错了什么? 请指导我,因为这是我第一次进行解析。

【问题讨论】:

    标签: iphone objective-c xml xml-parsing


    【解决方案1】:

    为什么不使用 XPath 查询呢?这会更容易,您可以解析循环中的每个项目。 我没有在您的 XML 上尝试此代码,但它应该可以工作:

    NSError *error;
    NSArray *xmlItems = [ddDoc nodesForXPath:@"//item" error:&error]; // where ddDoc is your DDXMLDocument
    
    for(DDXMLElement* itemElement in xmlItems)
    {
        // Here you get the item->value attribute value as string...
        NSString *itemValueAsString = [[itemElement attributeForName:@"value"]stringValue];
    
        // Now you get the image element from inside the item element
        DDXMLElement *imageElement = [[itemElement nodesForXPath:@"image" error:&error] objectAtIndex:0];
    
        // And finally the image->value attribute value as string…
        NSString *imageValueAsString = [[imageElement attributeForName:@"value"]stringValue];
    }
    

    希望它能解决你的问题。

    【讨论】:

    • 需要释放字符串吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多