+ (NSString *) xmlOfTest
{
GDataXMLElement * requestElement = [GDataXMLElement elementWithName:@"root"];
GDataXMLElement * accountElement = [GDataXMLElement elementWithName:@"account" ];
//[requestElement addChild:accountElement];//this should be at the last
[accountElement addAttribute:[GDataXMLNode elementWithName:@"random" stringValue: @"11"]];
[accountElement addChild:[GDataXMLElement elementWithName:@"mode" stringValue: @"22"]];
[accountElement addChild:[GDataXMLElement elementWithName:@"reqType" stringValue: @"333"]];
[requestElement addChild:accountElement];
NSString *request = [requestElement XMLString];
return request;
}
<root><account random="11"><mode>22</mode><reqType>333</reqType></account></root>
<root><account/></root>
+ (void) getTestDataFromXml:(NSString *)xml
{
NSError * parseError = nil;
/* 解析XML */
// <root><account random="11"><mode>22</mode><reqType>333</reqType></account></root>
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xml
options:0 error:&parseError];
NSArray *retcodeMembers = [doc.rootElement elementsForName:@"account"];
if (retcodeMembers.count > 0)
{
GDataXMLElement *retCodeResult = (GDataXMLElement *)[retcodeMembers objectAtIndex:0];
NSLog(@"%@", [[retCodeResult attributeForName:@"random"] stringValue]);
NSArray * tmpArray = [retCodeResult elementsForName:@"mode"];
NSLog(@"%@", [[tmpArray objectAtIndex:0] stringValue]);
}
[doc release];
}
2012-07-04 16:46:04.277 [96641:10a03] 11
2012-07-04 16:46:23.947 [96641:10a03] 22