0

我有一个具有以下结构的 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>和它的孩子。

4

2 回答 2

6

首先,我不得不说您的 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>

名为 <item_ 1>、<item_ 2> 的标签没有意​​义,因为 XML 旨在描述您的数据,而不是定义它。如果您有一个名为 item 的实体,您可能应该将其命名为item并使 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 获取您需要的节点,并假设它们的顺序正确,而忽略我指定的 index= 参数。

下一个问题是,将 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,然后如果遇到问题,请返回此处并提出具体问题。

于 2009-11-19T16:48:53.213 回答
0
    -(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 
于 2013-06-13T12:38:00.870 回答