Loading and parsing XML files

Source: Internet
Author: User

Load and parse XML file 1.XML format

<?xml version= "1.0" encoding= "Utf-8"?> represents the XML file version, encoding used for internal text

<root> represents the root node

<CityName> Beijing </CityName> Beijing A junction, CityName is the node name, Beijing Junction point value

<item key= "1" value= "A" ></Item> key= "1" is a node attribute, key property name, "1" property value

Note: The XML structure is understood as a layered nested tree structure

<?XML version= "1.0" encoding= "Utf-8"?><Root>    <Systemconfig>      <CityName>Beijing</CityName>      <Citycode>201</Citycode>      <Parentcitycode>0</Parentcitycode>      <AreaCode>010</AreaCode>      <Agreementurl></Agreementurl>      <Intentionlevel>                               <ItemKey= "1"value= "A"></Item>        <ItemKey= "2"value= "B"></Item>        <ItemKey= "3"value= "C"></Item>      </Intentionlevel>      <Comechannel>                                   <ItemKey= "1"value= "Newspaper"></Item>        <ItemKey= "2"value= "Magazine"></Item>      </Comechannel>      <Buycarbudget>                               <ItemKey= "1"value= "40.5 million"></Item>        <ItemKey= "2"value= "50.6 million"></Item>      </Buycarbudget>     <Intentioncolor>         <ItemKey= "1"value= "Red"></Item>         <ItemKey= "2"value= "Yellow"></Item>     </Intentioncolor>    </Systemconfig></Root>
2.XML parsing 1. Configure the XML library (to use after configuration)
    // (1) Add header file search path     //       Header Search paths->/usr/include/libxml2    //(2) Add binary library     //   Link Library-  lixml2.dylib    //(3) source file Add compilation options     //        -fno-objc-arc    //(4) Add header file     //  # Import "GDataXMLNode.h"

2.GData use
 //1. Loading and parsing XML filesNSString *path = [[NSBundle mainbundle] Pathforresource:@"Xml.txt"Oftype:nil]; NSData*data =[[NSData alloc] initwithcontentsoffile:path]; //gdataxmldocument represents an XML Document Object//Initwithdata using NSData initialization is the parsingGdataxmldocument *doc = [[Gdataxmldocument alloc] initwithdata:data options:0Error:nil]; //2. Get instruction Syntax XPath//cityname Path:/root/systemconfig/citynameNsarray*array = [Doc Nodesforxpath:@"/root/systemconfig/cityname"Error:nil]; Gdataxmlelement*element =[Array firstobject]; NSLog (@"name =%@ value =%@", Element.name,element.stringvalue); //gets the properties of the specified nodeNsarray *items = [Doc Nodesforxpath:@"/root/systemconfig/comechannel/item"Error:nil]; Gdataxmlelement*item =[Items Firstobject]; //Property , the attribute is represented using gdataxmlelement     for(Gdataxmlelement *attrinchitem.attributes) {NSLog (@"name =%@ value =%@", Attr.name,attr.stringvalue); }        //4. Get all nodes of the specified name, regardless of location//XPath Syntax://ItemNsarray*allitem = [Doc Nodesforxpath:@"//item"Error:nil];  for(Gdataxmlelement *einchAllitem) {NSLog (@"name =%@", e.name); }        //5. Gets all properties of the specified name, regardless of location//XPath Syntax://@valueNsarray *allvalue = [Doc Nodesforxpath:@"//@value"Error:nil];  for(Gdataxmlelement *einchallvalue) {NSLog (@"value =%@", E.stringvalue); }        //6. Iterate through the XML file layer by row//Get root node//gdataxmlelement *root = doc.rootelement; //Get child nodes//Root.children//gets the number of child nodes//Root.childrencount//Gets the child node of the specified name//Root Elementsforname
3. More detailed XML parsing syntax XPath syntax

Link URL: http://www.w3school.com.cn/xpath/xpath_syntax.asp

Loading and parsing XML files

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.