JSON and XML

Source: Internet
Author: User
Tags tag name

send request (URL)
1.client----------------"service side
Send data (Json/xml)
<---------------------------



1. xml Format

Common: plist file, NetEase news, Sina News

XML document

Tag/node <root></root> outermost root is called root node
Tag name Root

<CityName> Beijing </CityName>
Tag Value/node value Beijing

<item key= "1" value= "newspaper" > </Item>
key= "1" attribute node
Property: key= "1"
Property Name: Key
Property value: "1"

*xml data (document) top-down tree structure
*<root>.....</root> (a complete node), the name of the root node (the title) in the middle (...) For the content of the node
* Each XML document has only one root node (the outermost node in the tree structure)
*xml documents (data) are made up of multiple nodes (the child nodes, child nodes, such as the root node ...).
*/
Two-fork Tree


JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format with good readability and easy-to-write features. Data exchange between different platforms is possible. JSON uses a very high compatibility, completely independent of the language text format, at the same time have a similar C language habits (including C, C + +, C #, Java, JavaScript, Perl, Python, etc.) the behavior of the system.

These features make JSON an ideal data exchange language.



Xml
Extensible Markup Language (extensible Markup Language, XML), which is used to mark electronic files with a structured markup language. can be used to tag data, define data types. is a source language that consents to the user's definition of their markup language. XML uses a DTD (document type definition) to organize the data using the file type definitions, in a uniform format, across platforms and languages. has long been recognized as a standard in the industry

JSON (data relative to XML, lightweight, high transfer rate, less redundant data.) Cons: Poor readability)

XML (redundant data many, strengths: readable, strong extensibility)
/*xml Extensible Markup Language, mainly used for client and server data interaction.


*xml Data features: A top-down tree structure with only one root node:<root>...</root>
* The process of parsing XML data is the process of acquiring node content
*xml and json:xml data are readable and extensible, but data with no JSON format is lightweight and redundant data is more
*/

Nsjsons .....
Sbjson

1. System Nsxmlparser is more complex

2. Third party open Source Library GData often use or kissxml

Parsing XML often uses libraries: GData
The bottom uses the system's LIBXML2 library and joins LIBXML2 when used

(1) Import Gdata Library and drag it directly in
(2) Set header file search path
Build Setting--->header search Path
Join/USR/INCLUDE/LIBXML2

(3) Add libxml Binary Library
Build Phases---> Link Binary
Join Libxml2.dylib

(4) The assumption is that ARC's project
Build Phases---> Compile Source File
GDATAXMLNODE.M (non-ARC)

GData third-party libraries have two ways of parsing
1. Get root node all elements traverse all child nodes

2.xpath Syntax


2. XPath gets the specified node
@ "http://10.0.8.8/sns/my/user_list.php?format=xml&page=1&number=10" to get the data in XML format

Sina News
Http://rss.sina.com.cn/sina_all_opml.xml


The function of mastering


Gdataxmlnode

Gets the value of the current node
-(NSString *) stringvalue;
Gets the number of node nodes in the current section
-(Nsuinteger) ChildCount;
Get an array of child nodes
-(Nsarray *) children;
Get child nodes by index
-(Gdataxmlnode *) Childatindex: (unsigned) index;

Gdataxmlelement
Get all node arrays with node name named name based on node name
-(Nsarray *) Elementsforname: (NSString *) name;
Get the properties of a node
-(Nsarray *) attributes;
Get attribute nodes by property name
-(Gdataxmlnode *) Attributeforname: (NSString *) name;

Gdataxmldocument
Create a document tree
-(ID) Initwithdata: (NSData *) data encoding: (nsstringencoding) Encoding error: (NSERROR *) error;
Get and Node
-(Gdataxmlelement *) rootelement;

Gets the specified array of nodes based on the XPath syntax
-(Nsarray *) Nodesforxpath: (NSString *) XPath error: (NSERROR * *) error;

Functions that the extended function adds on Gdata

Add by Lzx 2014-05-28
We are able to extend some new methods that are not available to this open source library.
Then just import the current file's source code so that you can directly use the

Gets the text information (value) of the child node of the current node with the specified name, name (only a child node called name)
-(NSString *) Stringvaluebyname: (nsstring*) subname;
Gets the text information (value) of the child node of the current node named name name (assuming there are multiple child nodes with the same name)
-(Nsarray *) Stringvaluesbyname: (nsstring*) subname;
Gets the value of the specified name of the current node Attrname property
-(nsstring*) Attributevaluebyname: (nsstring*) Attrname;
Gets the value of all child nodes in the specified name of the current node (the names of all child nodes to be obtained in the array)
-(Nsdictionary *) Subdictwitharray: (nsarray*) subnames;

JSON and XML

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.