【问题标题】:TouchXML parsing XML attributesTouchXML 解析 XML 属性
【发布时间】:2010-11-02 04:11:18
【问题描述】:

如何使用 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 示例中,我设法获得了 Player 属性,但没有获得子节点的“RoundScore”属性。

希望能伸出援手..

谢谢,

【问题讨论】:

    标签: iphone xml cocoa-touch touchxml


    【解决方案1】:

    更多信息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;
                }
            );
        }
    )
    

    【讨论】:

    • ,谢谢。它真的帮助了我。
    • @Spark。 nodeforXpath 是必要的吗?我想将一些数据发布到服务器?我从服务器获取数据没问题,但我无法将其发布或更新到服务器。我有网络服务。
    【解决方案2】:

    使用 TBXML,它在处理 XML 文件时更加容易和快捷。也不错的文档。您的属性问题在这里更容易解决。 http://www.tbxml.co.uk/TBXML/TBXML_Free.html

    【讨论】:

      【解决方案3】:

      我发现您必须检查子节点是否为 CXElements。有时它们是 CXMLNode,它们没有属性属性。

      【讨论】:

      • 你能举个例子吗?我对解析 XML 标签属性有同样的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2010-11-12
      相关资源
      最近更新 更多