2009-11-19 5 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을 사용하기가 어려울 수 있습니다. 예를 들어 XML이 그것을 정의하지, 당신의 데이터를 설명하기위한 것입니다으로 < item_ 1 > 이름

<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_ 2 >는 이해가되지 않습니다. 당신이 엔티티 항목을 호출하는 경우, 당신은 아마 항목를 호출하고 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