我有一个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读取年龄和名称?
解决方法Nsstring *name = [[partyMember attributeForname:@"name"] stringValue];总结
以上是内存溢出为你收集整理的iphone – 使用GDataXMLDocument解析xml属性全部内容,希望文章能够帮你解决iphone – 使用GDataXMLDocument解析xml属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)