4
如何使用GDataXMLDocument解析xml屬性?使用GDataXMLDocument解析xml屬性
我有一個XML字符串作爲NSData的(命名爲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)的屬性。
<students>
<student name="Raju" age = 25>
</student>
</students>
如何使用GDataXMLDocument讀取年齡和名稱?