本文介绍了使用GDataXMLDocument解析xml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用GDataXMLDocument解析xml属性?

How do I parse xml attribute using GDataXMLDocument?

我有一个xml字符串作为NSData(命名为xmlData)

I have an xml string as NSData(named as xmlData )

<students>
 <student>
       <name>Raju</name>
       <age>25</age>
 </student>
</students>

然后我读了它:

 NSError *error;

 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    if (doc == nil) { return ; }


    //NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];
    NSArray *partyMembers = [doc nodesForXPath:@"//students/student" error:nil];
    for (GDataXMLElement *partyMember in partyMembers) {

        // Let's fill these in!
        NSString *name;
        int age;


        // Name
        NSArray *names = [partyMember elementsForName:@"name"];
        if (names.count > 0) {
            GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
            name = firstName.stringValue;
   NSLog([NSString stringWithFormat:@"name is: %@",name]);
        } else continue;

        // Level
        NSArray *ages = [partyMember elementsForName:@"age"];
        if (ages.count > 0) {
            GDataXMLElement *firstLevel = (GDataXMLElement *) [ages objectAtIndex:0];
            age = firstLevel.stringValue.intValue;
   NSLog([NSString stringWithFormat:@"age  is: %d",age]);
        } else continue;

    }

    [doc release];

}

我得到名称年龄

现在我有一个带属性的xml(作为NSData)。

Now I have an xml(as NSData) with attributes.

<students>
 <student name="Raju" age = 25>

 </student>
</students>

如何使用GDataXMLDocument读取年龄和姓名?

How do I read the age and name using GDataXMLDocument ?

推荐答案

NSString *name = [[partyMember attributeForName:@"name"] stringValue];

这篇关于使用GDataXMLDocument解析xml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:12