2009-06-11 101 views
7

如何使用touchXML解析此XML?我想將所有屬性作爲鍵/值對存儲在字典中。TouchXML解析XML屬性

<Player PlayerName="Padraig HARRINGTON" CurrentPosition="1" CurrentRank="1" 
    Country="IRL" NumberOfHolesPlayed="18" ParRelativeScore="+3"> 
    <RoundScore RoundNumber="1" Score="74" /> 
    <RoundScore RoundNumber="2" Score="68" /> 
    <RoundScore RoundNumber="3" Score="72" /> 
    <RoundScore RoundNumber="4" Score="69" /> 
</Player> 
<Player PlayerName="Ian POULTER" CurrentPosition="2" CurrentRank="2" Country="ENG" 
    NumberOfHolesPlayed="18" ParRelativeScore="+7"> 
    <RoundScore RoundNumber="1" Score="72" /> 
    <RoundScore RoundNumber="2" Score="71" /> 
    <RoundScore RoundNumber="3" Score="75" /> 
    <RoundScore RoundNumber="4" Score="69" /> 
</Player> 
<Player PlayerName="Henrik STENSON" CurrentPosition="3" CurrentRank="T3"   Country="SWE" 
    NumberOfHolesPlayed="18" ParRelativeScore="+9"> 
    <RoundScore RoundNumber="1" Score="76" /> 
    <RoundScore RoundNumber="2" Score="72" /> 
    <RoundScore RoundNumber="3" Score="70" /> 
    <RoundScore RoundNumber="4" Score="71" /> 
</Player> 

我沒有問題,是XML的格式,像這樣:

<Player> 
<Country>UK</Country> 
<NumberOfHolesPlayed>12</NumberOfHolesPlayed> 
... 
... 

但我不知道帶有屬性打交道時該怎麼辦...

如何通過touchXML獲取屬性?特別是如果節點具有也具有屬性的子節點。

根據第一個示例XML文件。在第一個XML示例中,我設法獲取播放器屬性,但未設置子節點的「RoundScore」屬性。

很想伸出援手..

感謝,

回答

0

我發現你要檢查孩子節點CXElements。有時它們是CXMLNode,並且它們沒有屬性屬性。

+0

那麼你的問題得到了解答?如果是這樣,請標記。 – JoePasq 2009-10-30 01:38:57

+0

你能舉個例子嗎?我有關於解析XML標籤屬性的相同問題。 – sniurkst 2009-11-27 12:45:36

13

欲瞭解更多信息visit this post。我已經給出了完整的簡要說明。

是的!解決了你的問題。

請看下面的代碼。希望你能理解。 它符合您的要求。我也添加了 - NSLog - 結果 - 解析。

-(void)methodForParsingPlayers{ 
    NSMutableArray *ar=[[NSMutableArray alloc] init]; 
    CXMLDocument *doc=[[[CXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Players" ofType:@"xml"]] options:0 error:nil] autorelease]; 

    NSArray *nodes=nil; 
    nodes=[doc nodesForXPath:@"//Player" error:nil]; 

    NSString *strValue; 
    NSString *strName; 

    for (CXMLElement *node in nodes) { 
     NSMutableDictionary *object=[[NSMutableDictionary alloc] init]; 

     // process to set attributes of object ---------------------------------------- 
     NSMutableDictionary *objectAttributes=[[NSMutableDictionary alloc] init]; 
     NSArray *arAttr=[node attributes]; 
     NSUInteger i, countAttr = [arAttr count]; 
     for (i = 0; i < countAttr; i++) { 
      strValue=[[arAttr objectAtIndex:i] stringValue]; 
      strName=[[arAttr objectAtIndex:i] name]; 
      if(strValue && strName){ 
       [objectAttributes setValue:strValue forKey:strName]; 
      } 
     } 
     [object setValue:objectAttributes forKey:[node name]]; 
     [objectAttributes release]; objectAttributes=nil; 
     // -------------------------------------------------------------------------------- 

     // process to read elements of object ---------------------------------------- 
     NSUInteger j, countElements = [node childCount]; 
     CXMLNode *element; 
     NSMutableDictionary *elementDictionary=nil; 
     for (j=0; j<countElements; j++) { 
      element=[node childAtIndex:j]; 
      elementDictionary=[[NSMutableDictionary alloc] init]; 

      // process to read element attributes ---------------------------------- 
      if([element isMemberOfClass:[CXMLElement class]]){ 
       CXMLElement *element2=(CXMLElement*)element; 
       arAttr=[element2 attributes]; 
       countAttr=[arAttr count]; 
       for (i=0; i<countAttr; i++) { 
        strName=[[arAttr objectAtIndex:i] name]; 
        strValue=[[arAttr objectAtIndex:i] stringValue]; 
        if(strName && strValue){ 
         [elementDictionary setValue:strValue forKey:strName]; 
        } 
       } 
      } 
      // -------------------------------------------------------------------- 

      // element value if available 
      strValue=[element stringValue]; 
      if(strValue){ 
       [elementDictionary setValue:strValue forKey:@"value"]; 
      } 
      // --------------------------------------------------------------------- 

      // check if object/dictionary exists for this key "name" 
      strName=[element name]; 
      if([object valueForKey:strName]){ 
       if([[object valueForKey:strName] isKindOfClass:[NSMutableDictionary class]]){ 
        NSMutableDictionary *d=[[NSMutableDictionary alloc] initWithDictionary:[object valueForKey:strName]]; 
        NSMutableArray *arOFSameElementName=[[NSMutableArray alloc] initWithObjects:d,elementDictionary,nil]; 
        [object setValue:arOFSameElementName forKey:strName]; 
        [d release]; d=nil; 
        [arOFSameElementName release]; arOFSameElementName=nil; 
       } else { 
        NSMutableArray *arOFSameElementName=[object valueForKey:strName]; 
        [arOFSameElementName addObject:elementDictionary]; 
       } 
      } else { 
       [object setValue:elementDictionary forKey:strName]; 
      } 
      [elementDictionary release]; elementDictionary=nil; 
      // --------------------------------------------------------------------- 
     } 
     [ar addObject:object]; 
     [object release]; object=nil; 
     // -------------------------------------------------------------------------------- 
    } 
    NSLog(@"%@",[ar description]); 
} 


2010-08-13 12:45:48.786 TouchTry[2850:207] (
     { 
     Player =   { 
      Country = IRL; 
      CurrentPosition = 1; 
      CurrentRank = 1; 
      NumberOfHolesPlayed = 18; 
      ParRelativeScore = "+3"; 
      PlayerName = "Padraig HARRINGTON"; 
     }; 
     RoundScore =   (
         { 
       RoundNumber = 1; 
       Score = 74; 
      }, 
         { 
       RoundNumber = 2; 
       Score = 68; 
      }, 
         { 
       RoundNumber = 3; 
       Score = 72; 
      }, 
         { 
       RoundNumber = 4; 
       Score = 69; 
      } 
     ); 
    }, 
     { 
     Player =   { 
      Country = ENG; 
      CurrentPosition = 2; 
      CurrentRank = 2; 
      NumberOfHolesPlayed = 18; 
      ParRelativeScore = "+7"; 
      PlayerName = "Ian POULTER"; 
     }; 
     RoundScore =   (
         { 
       RoundNumber = 1; 
       Score = 72; 
      }, 
         { 
       RoundNumber = 2; 
       Score = 71; 
      }, 
         { 
       RoundNumber = 3; 
       Score = 75; 
      }, 
         { 
       RoundNumber = 4; 
       Score = 69; 
      } 
     ); 
    }, 
     { 
     Player =   { 
      Country = SWE; 
      CurrentPosition = 3; 
      CurrentRank = T3; 
      NumberOfHolesPlayed = 18; 
      ParRelativeScore = "+9"; 
      PlayerName = "Henrik STENSON"; 
     }; 
     RoundScore =   (
         { 
       RoundNumber = 1; 
       Score = 76; 
      }, 
         { 
       RoundNumber = 2; 
       Score = 72; 
      }, 
         { 
       RoundNumber = 3; 
       Score = 70; 
      }, 
         { 
       RoundNumber = 4; 
       Score = 71; 
      } 
     ); 
    } 
)