IOS之XML解析

來源:互聯網
上載者:User

標籤:

IOS之XML解析1.XML格式
<?xml version="1.0" encoding="utf-8" ?><root>    <systemConfig>      <CityName>北京</CityName>      <CityCode>201</CityCode>      <ParentCityCode> 0</ParentCityCode>      <areaCode>010</areaCode>      <AgreementUrl></AgreementUrl>      <IntentionLevel>                               <Item key="1" value="A"></Item>        <Item key="2" value="B"></Item>        <Item key="3" value="C"></Item>      </IntentionLevel>      <ComeChannel>                                   <Item key="1" value="報紙"></Item>        <Item key="2" value="雜誌"></Item>      </ComeChannel>      <BuyCarBudget>                               <Item key="1" value="40-50萬"></Item>        <Item key="2" value="50-60萬"></Item>      </BuyCarBudget>     <IntentionColor>         <Item key="1" value="紅"></Item>         <Item key="2" value="黃"></Item>     </IntentionColor>    </systemConfig></root>
  

  <?xml version="1.0" encoding="utf-8" ?> 表示XML檔案版本, 內部文本使用的編碼

  <root> 表示根節點

  <CityName>北京</CityName>  一個結點, CityName是結點名, 北京結點值

  <Item key="1" value="A"></Item>     key="1"是結點屬性, key屬性名稱, "1"屬性值

    注意: XML結構理解為層層嵌套的樹形結構

 2.如何使用GData開源庫實現XML解析

  常用的XML解析方式

    (1)unix - libxml2 C介面

    (2)NSXMLParser UI提供的XML解析庫, 使用比較麻煩

    (3)google開發GData開源庫

  2.1 GData的配置
//配置XML庫(配置完才能使用)    //(1)添加標頭檔搜尋路徑    //      PROJECT->Build Settings->Search Paths->Header Search Paths-> /usr/include/libxml2    //(2)添加二進位庫    //      TARGETA-> Build Phases->Link Binary With Libraries-> “+” ->  libxml2.dylib->Add    //(3)源檔案添加編譯選項    //     在工程TARGETS-> Build Phases-> Compile Sources->搜尋GData並在後面加上  “-fno-objc-arc”    //(4)添加標頭檔    //    #import "GDataXMLNode.h"
  2.1GData的使用
//    1.載入和解析XML檔案    NSString *path = [[NSBundle mainBundle]pathForResource:@"xml.txt" ofType:nil];    NSData *data = [[NSData alloc]initWithContentsOfFile:path];        //    GDataXMLDocument 表示XML文檔對象    //    initWithData 使用NSData初始化,就是解析    GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];    //    2.擷取指定結點 XPath    //CityName路徑:/root/systemConfig/CityName    NSArray *CityNameArray = [doc nodesForXPath:@"/root/systemConfig/CityName" error:nil];    GDataXMLElement *element = [CityNameArray firstObject];    NSLog(@"name = %@  value = %@",element.name,element.stringValue);        //3.擷取指定結點的屬性    NSArray *items = [doc nodesForXPath:@"/root/systemConfig/ComeChannel/Item" error:nil];    GDataXMLElement *item = [items firstObject];    //擷取屬性,屬性使用GDataXMLElement表示    for(GDataXMLElement *attr in item.attributes){        NSLog(@"name = %@  value = %@",attr.name,attr.stringValue);            }//    for (int i =0; i<2; i++) {//        GDataXMLElement *item = [items objectAtIndex:i];//        //擷取屬性,屬性使用GDataXMLElement表示//        for(GDataXMLElement *attr in item.attributes){//            NSLog(@"name = %@  value = %@",attr.name,attr.stringValue);//            //        }////    }    //4.擷取所有指定名字的結點(不管位置)    //XPath文法: //Item    NSArray *allItem = [doc nodesForXPath:@"//Item" error:nil];    for (GDataXMLElement *e  in allItem) {        NSLog(@"name = %@ ",e.name);    }    //5.擷取所有指定名字的結點(不管位置)    //XPath文法: //value    NSArray *allValue = [doc nodesForXPath:@"//@value" error:nil];    for (GDataXMLElement *e  in allValue) {        NSLog(@"value = %@ ",e.stringValue);    }    //6.逐層遍曆XML檔案    //擷取根節點    GDataXMLElement *root = doc.rootElement;//    //擷取子結點//    root.children//    //擷取子節點個數//    root.childCount//    //擷取指定名字的子節點//    root elementsForName:<#(NSString *)#>

 

 

 

IOS之XML解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.