IOS creates and parses XML with Gdataxmlnode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.