Gdataxml 例子

<Party>

  <Player>

    <Name>Butch</Name>

    <Level>1</Level>

    <Class>Fighter</Class>

  </Player>

  <Player>

    <Name>Shadow</Name>

    <Level>2</Level>

    <Class>Rogue</Class>

  </Player>

  <Player>

    <Name>Crak</Name>

    <Level>3</Level>

    <Class>Wizard</Class>

  </Player>

</Party>

NSError *error;

NSData* xmlData= [xmlString dataUsingEncoding: NSASCIIStringEncoding];

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];

if (doc == nil) { return nil; }

//NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];

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

for (GDataXMLElement *partyMember in partyMembers) {

// Let's fill these in!

NSString *name;

int level;

// Name

NSArray *names = [partyMember elementsForName:@"Name"];

if (names.count > 0) {

GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];

name = firstName.stringValue;

} else continue;

// Level

NSArray *levels = [partyMember elementsForName:@"Level"];

if (levels.count > 0) {

GDataXMLElement *firstLevel = (GDataXMLElement *) [levels objectAtIndex:0];

level = firstLevel.stringValue.intValue;

} else continue;

// Class

NSArray *classes = [partyMember elementsForName:@"Class"];

if (classes.count > 0) {

GDataXMLElement *firstClass = (GDataXMLElement *) [classes objectAtIndex:0];

if ([firstClass.stringValue caseInsensitiveCompare:@"Fighter"] == NSOrderedSame)

{

} else if ([firstClass.stringValue caseInsensitiveCompare:@"Rogue"] == NSOrderedSame) {

} else if ([firstClass.stringValue caseInsensitiveCompare:@"Wizard"] == NSOrderedSame) {

} else {

continue;

}            

} else continue;

}

[doc release];

[xmlData release];

posted on 2011-08-06 16:42  风乔  阅读(198)  评论(0编辑  收藏  举报

导航