我有以下结构的XML,我试图创建一个从这个我的模型对象。有人可以帮我找到一种方法,使用TouchXML,NSMutableArray里和NSMutableDictionay的XML得到这些对象。

<?xml version="1.0" encoding="utf-8"?>
<response>
  <level1_items>
    <level1_item>
      <item_1>text</item_1>
      <item_2>text</item_2>
    </level1_item>
    <level1_item>
      <item_1>some text</item_1>
      <item_2>some more text</item_2>
  </level1_items>
  <items>
    <item>
      <child_items>
        <child_item>
          <leaf1>node text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>some text</leaf3>
        </child_item>
        <child_item>
          <leaf1>text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>more text</leaf3>
        </child_item>
      </child_items>
    </item>
    <item>
      <child_items>
        <child_item>
          <leaf1>node text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>some text</leaf3>
        </child_item>
        <child_item>
          <leaf1>text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>more text</leaf3>
        </child_item>
      </child_items>
    </item>
  </items>
</response>

我需要解析<response>及其子

有帮助吗?

解决方案

首先,我不得不说,你的XML将是很难用,因为你正试图在列表中使用特定的标签为每个项目的工作。例如:

  <level1_items>
    <level1_item>
      <item_1>text</item_1>
      <item_2>text</item_2>
    </level1_item>
    <level1_item>
      <item_1>some text</item_1>
      <item_2>some more text</item_2>
  </level1_items>

命名标签,<2 ITEM_>是没有意义的XML是用来描述你的数据,而不是定义它。如果你有一个实体,称为一个项目,你应该只需要调用它的项目的,使XML是这样的:

  <level1_items>
    <level1_item>
      <item index="1">text</item>
      <item index="2">text</item>
    </level1_item>
    <level1_item>
      <item index="1">some text</item>
      <item index="2">some more text</item>
  </level1_items>

其中每个的具有索引。对于这个问题,你可以只加载文档在TouchXML CXMLDocument抢你需要使用XPath节点,并假定他们在正确的顺序无视我指定的索引=参数。

接下来的问题是,将XML转换到字典的NSDictioanry或NSArray的是一个漂亮参与的任务,你收获了什么是不值得的努力。只要您的XML加载到CXMLDocument,然后使用XPath开始获取节点。是这样的:

CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];

// Returns all 'level1_item' nodes in an array    
NSArray *nodes = [[doc rootElement] nodesForXPath:@"//response/level1_items/level1_item" error:nil];

for (CXMLNode *itemNode in nodes)
{
    for (CXMLNode *childNode in [itemNode children])
    {
        NSString *nodeName = [childNode name]; // should contain item_1 in first iteration
        NSString *nodeValue = [childNode stringValue]; // should contain 'text' in first iteration
        // Do something with the node data.

    }
}

我建议您尝试使用XML这种方式,然后再回到这里,并询问具体的问题,如果你有问题。

其他提示

    -(NSMutableArray *)parseNodeFromXML:(NSString *)strXML readForNode:(NSString *)strReadValue
    {
        NSError *theError = NULL;
        CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:strXML options:0 error:&theError] ;

        NSMutableArray *arrReturn = [[NSMutableArray alloc] init];
        NSArray *nodes = [[theXMLDocument rootElement] nodesForXPath:strReadValue error:nil];

        for (CXMLNode *itemNode in nodes)
        {

            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

            // PROCESS FOR READ ELEMENT ATTRIBUTES IN CURRENT NODE -------------------------

            if([itemNode isMemberOfClass:[CXMLElement class]]){
                CXMLElement *elements=(CXMLElement*)itemNode;
                NSArray *arAttr=[elements attributes];

                for (int i=0; i<[arAttr count]; i++) {
                    if([[arAttr objectAtIndex:i] name] && [[arAttr objectAtIndex:i] stringValue]){
                        [dic setValue:[[arAttr objectAtIndex:i] stringValue] forKey:[[arAttr objectAtIndex:i] name]];
                    }
                }
            }


            // PROCESS FOR READ CHILD NODE IN CURRENT NODE -------------------------

            for (CXMLNode *childNode in [itemNode children])
            {
                //   NSLog(@"count -- %d ---  %@",[childNode childCount],[childNode children]);

                // IF ANY CHILD NODE HAVE MULTIPLE CHILDRENS THEN DO THIS....- 
                if ([childNode childCount] > 1)
                {
                    NSMutableDictionary *dicChild = [[NSMutableDictionary alloc] init];
                    for (CXMLNode *ChildItemNode in [childNode children])
                    {
                        [dicChild setValue:[ChildItemNode stringValue] forKey:[ChildItemNode name]];
                    }
                     [dic setValue:dicChild forKey:[childNode name]];
                }else
                {
                    [dic setValue:[childNode stringValue] forKey:[childNode name]];
                }

            }

            [arrReturn addObject:dic];
        }

        //    NSLog(@"%@",arrReturn);
        return arrReturn;
    }


may this help for multiple get child 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top