IOS資料解析之XML

來源:互聯網
上載者:User

標籤:

XML

1.定義

  • 指可延伸標記語言 (XML)()
  • XML HTML
  • XML 傳輸資料,而非顯示資料
  • XML XML 被設計為具有自我描述性。
<?xml version="1.0" encoding="utf-8"?><Users>    <User>        <name id = "201502004">張三</name>        <age>24</age>    </User>    <User>        <name id = "201502002">李四</name>        <age>23</age>    </User></Users>

2.解析

1) KissXML

*需匯入KissXML庫

*進入Build Settings,在搜尋方塊中搜尋Head Search Path,然後雙擊並點擊+按鈕添加/usr/include/libxml2

*然後搜尋Other linker flags,同樣的方式添加-lxml2

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // 1.擷取xml地址    NSString *path = [[NSBundle mainBundle]pathForResource:@"user" ofType:@"xml"];    // 2.獲得本地urlString    NSString *content = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path] encoding:NSUTF8StringEncoding error:nil];    // 3.解析    DDXMLDocument *document = [[DDXMLDocument alloc]initWithXMLString:content options:0 error:nil];    // 4.通過xpath找到指定節點    NSArray *elements = [document nodesForXPath:@"/Users/User" error:nil];    // 5.遍曆    for (DDXMLElement *element in elements) {        // 取到name        DDXMLElement *name = [element elementsForName:@"name"][0];        // 得到name的屬性        NSArray *node = [document nodesForXPath:@"/Users/User/name" error:nil];        DDXMLNode *nodeID = [node[0] attributeForName:@"id"];        // 得到age        DDXMLElement *age = [element elementsForName:@"age"][0];        NSLog(@"name = %@, age = %@, id = %@",[name stringValue],[age stringValue],[nodeID stringValue]);    }}

2)GData(MRC下)

*匯入GDataXMLNode庫

* 找到Build OPhases - > Compile Sources ->GDataXMLNode.m雙擊添加 -fno-objc-arc

*進入Build Settings,在搜尋方塊中搜尋Head Search Path,然後雙擊並點擊+按鈕添加/usr/include/libxml2

*然後搜尋Other linker flags,同樣的方式添加-lxml2

- (void)GData{    // 1.獲得path    NSString *path = [[NSBundle mainBundle]pathForResource:@"user" ofType:@"xml"];    // 2.得到content    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    // 3.得到節點    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:content options:0 error:nil];    NSArray *elements = [document nodesForXPath:@"/Users/User" error:nil];    for (GDataXMLElement *element in elements) {        // 取到name        GDataXMLElement *name = [element elementsForName:@"name"][0];        // 得到name的屬性        NSArray *node = [document nodesForXPath:@"/Users/User/name" error:nil];        GDataXMLElement *nodeID = [node[0] attributeForName:@"id"];        // 得到age        GDataXMLElement *age = [element elementsForName:@"age"][0];        NSLog(@"name = %@, age = %@, id = %@",[name stringValue],[age stringValue],[nodeID stringValue]);    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

3.定義XML

*使用KissXML

- (void)createXML{    // 1.建立根項目    DDXMLElement *rootElement = [[DDXMLElement alloc] initWithName:@"Users"];    for (int i = 0; i < 2; i++)    {        // 2.User子項目        DDXMLElement *userElement = [[DDXMLElement alloc] initWithName:@"User"];        // name元素        DDXMLElement *nameElement = [[DDXMLElement alloc] initWithName:@"name" stringValue:[NSString stringWithFormat:@"張三%d",i+1]];        // age元素        DDXMLElement *ageElement = [[DDXMLElement alloc] initWithName:@"age" stringValue:[NSString stringWithFormat:@"%d",20+i]];        // 5.添加子項目        [userElement addChild:nameElement];        [userElement addChild:ageElement];        // 6.在nameElement中添加屬性        DDXMLNode *node = [DDXMLNode attributeWithName:@"id" stringValue:@"xxxx"];        [nameElement addAttribute:node];        // 7.把User添加到根項目中        [rootElement addChild:userElement];    }    NSLog(@"--%@",[rootElement description]);}

 

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.