2009-11-19 63 views
0

我有一個帶有以下結構的XML,我試圖從中創建我的模型對象。有人可以幫助我找到一種方法,使用TouchXML,NSMutableArray和NSMutableDictionay從XML中獲取這些對象。使用TouchXML解析多個和多級嵌套元素

<?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>及其子。

回答

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是用來描述你的數據,而不是定義它沒有任何意義。如果你有一個實體,稱爲一個項目,你應該叫它項目,使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這種方式,然後回來這裏,並問如果你有問題的具體問題。

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