您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

GDataXMLNode:在 iOS 中解析 XML 的實用筆記

編輯:iOS 相關資訊

GDataXMLNode 是 Google 提供的 XML 處理工具,底層封裝 libxml2 的 DOM 方式,適合讀寫中小型 XML 文件,也支援 XPath。它常用於需要快速解析 plist 以外 XML 結構的 iOS 專案。

加入方式

  1. 將 GDataXMLNode.h 與 GDataXMLNode.m 加入專案。
  2. 加入 libxml2.dylib 或對應的 libxml2 函式庫。
  3. 在 Build Settings 的 Header Search Paths 加入 /usr/include/libxml2。
  4. 匯入 GDataXMLNode.h,確認可編譯。

解析範例

<root>
  <name value="wusj"/>
  <age>24</age>
</root>
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
GDataXMLElement *root = [doc rootElement];
for (GDataXMLElement *ele in [root children]) {
    if ([[ele name] isEqualToString:@"name"]) {
        NSLog(@"name: %@", [[ele attributeForName:@"value"] stringValue]);
    } else {
        NSLog(@"age: %@", [ele stringValue]);
    }
}

常用 API 包括 name 取得標籤名稱、attributeForName: 取得屬性節點,再以 stringValue 取值;stringValue 則可取得標籤之間的文字內容。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved