XML and JSON for network parsing, and xmljson for parsing

Source: Internet
Author: User

XML and JSON for network parsing, and xmljson for parsing

First, add the class library GDataXMLNode and JSON

Students.txt

<Students>

<Student>

<Name> Tom </name>

<Age> 20 </age>

<Phone> 13049640144 </phone>

</Student>

<Student>

<Name> Jimmy </name>

<Age> 20 </age>

<Phone> 13049640144 </phone>

</Student>

<Student>

<Name> screw </name>

<Age> 20 </age>

<Phone> 13049640144 </phone>

</Student>

</Students>

// XML Parsing

 

NSString * str = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "Students" ofType: @ "txt"] encoding: NSUTF8StringEncoding error: nil];

 

// Parse XML and put the result in the document

GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithXMLString: str options: 0 error: nil];

GDataXMLElement * root = [document rootElement]; // obtain the root node

 

NSArray * arr = [root nodesForXPath: @ "// name" error: nil];

// Traverse nodes

For (GDataXMLElement * name in arr)

{

NSLog (@ "% @", [name stringValue]);

}


// Json Parsing

NSString * str = @ "[{\" name \ ": \" kelly \ ", \" age \ ": \" 23 \ ", \" sex \": \ "female \" },{ \ "name \": \ "evnxy \", \ "age \": \ "21 \", \ "sex \": \ "female \"}] ";

NSArray * arr = [str JSONValue];

NSLog (@ "% @", arr );

Related Article

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.