Original address: http://blog.csdn.net/gf771115/article/details/7718403
Nserror *error;
NSString *path = [[NSBundle mainbundle] pathforresource:@ "Testxml" oftype:@ "xml"];
NSString *content = [[NSString alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
NSLog (content);
Gdataxmldocument *document = [[Gdataxmldocument alloc] initwithxmlstring:content options:1 error:&error];
Creating XML
Gdataxmlelement *rootelement = [Gdataxmlnode elementwithname:@ "Request"];
Gdataxmlelement *element = [self getelementwithname:@ "Peter" withage:@ "" withsex:@ "male" withattribute:@ "0"];
[RootElement addchild:element];
element = [Self getelementwithname:@ "Green" withage:@ "withsex:@" female "withattribute:@" 1 "];
[RootElement addchild:element];
element = [Self getelementwithname:@ "Susan" withage:@ "withsex:@" female "withattribute:@" 1 "];
[RootElement addchild:element];
Gdataxmldocument *document = [[Gdataxmldocument alloc] initwithrootelement:rootelement];
NSData *data = [Document XMLData];
NSString *content = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (content);
if (Error) {
NSLog ([Error localizeddescription]);
}else{
Parsing xml
Gdataxmlelement *root = [Document RootElement];
for (int i=0; I <[rootelement ChildCount]; i++) {
Gdataxmlelement *element = [Root childatindex:i];
NSLog (@ "Element Name =%@", element.name);
NSLog (@ "type =%@", [[Element attributeforname:@ "type"] stringvalue]);
for (int j=0; j<[element ChildCount]; j + +) {
Gdataxmlelement *subitem = [element childatindex:j];
NSLog (@ "Sub Name =%@", subitem.name);
NSLog (@ "Sub value =%@", subitem.stringvalue);
}
}
}
IOS creates and parses XML with Gdataxmlnode