Introduction to various platforms for parsing XML ios-data

Source: Internet
Author: User
Tags xml parser

In the iphone development, XML parsing has many options, the IOS SDK provides Nsxmlparser and libxml2 two class libraries, there are also many third-party class libraries are optional, such as Tbxml, Touchxml, Kissxml, TinyXML and Gdataxml. The question is which one should we choose?

Parsing XML usually has two ways, DOM and SAX:

    • When the DOM parses the XML, it reads into the entire XML document and constructs a tree structure (node tree) that resides in memory, which can be retrieved from any XML node by traversing the tree structure to read its properties and values. And, in general, you can query XML nodes directly with the help of XPath.
    • Sax parsing XML, is based on the event notification mode, while reading the XML document side processing, do not have to wait for the entire document to be loaded before taking action, when the read parsing process encountered the need to handle the object, will be issued a notification to handle it.

Generally in the iOS platform, the more commonly used XML parsing class library has the following several:

  • nsxmlparser,http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/classes/ Nsxmlparser_class/reference/reference.html, which is a class library of Sax parsing XML, is included by default in the iOS SDK and is easier to use.
  • libxml2,http://xmlsoft.org/, a set of open-source libraries that are included in the iOS SDK by default, is a C-based API, so it might not be as easy to use as Nsxml. This class library supports both DOM and sax parsing, LIBXML2 sax parsing mode is very cool, because it can read edge parsing, especially in downloading a large XML file from the Internet, you can download the content on the side of the download to parse, greatly improve the resolution efficiency.
  • Tbxml,http://www.tbxml.co.uk/tbxml/tbxml_ Free.html, which is a lightweight DOM-style XML parsing class library, has good performance and low memory footprint, but it does not validate XML format, does not support XPath, and only supports parsing, and does not support modification of XML.
  • Touchxml,https://github.com/touchcode/touchxml, which is also a set of DOM-style XML parsing class libraries that support XPath and do not support XML modification.
  • Kissxml,http://code.google.com/p/kissxml/, this is a set of XML parsing class libraries based on Touchxml, which supports XML modification compared to Touchxml.
  • Tinyxml,http://www.grinninglizard.com/tinyxml/, this is a small set of C-based DOM methods for XML parsing of the class library, support for XML reading and modification, does not directly support XPath, It is necessary to use another related class library Tinyxpath to support XPath.
  • gdataxml,http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/source/xmlsupport/, This is a set of Google-developed DOM way XML parsing class library, support to read and modify XML documents, support XPath query.
  • So how do you choose the right XML parsing class library in your project? Some of these XML libraries have been analyzed and compared on the internet, so refer to the "How to Choose" Best XML Parser for Your iPhone Project http://www.raywenderlich.com/553/ How-to-chose-the-best-xml-parser-for-your-iphone-project A article, basically more accurate and objective, the article suggests:
  • If you are reading very small XML documents, there is basically no difference in performance, but it is recommended to use Touchxml, Kissxml, or gdataxml from the convenience of invocation.
  • If you need to read and modify an XML document, it is recommended to use Kissxml or Gdataxml
  • If you need to read very large XML documents, it is recommended to use LIBXML2 or Tbxml
  • If you don't want to call a third-party class library, you can also use Nsxml
Installation InstructionsGdataxml

1. Download "Gdata-objective-c client library" from Http://code.google.com/p/gdata-objectivec-client/downloads/list.

2. Unzip the file, locate the Source\xmlsupport, and drag the GDataXMLNode.h and GDATAXMLNODE.M files into the project

3. Select the project and select the "Build Settings" tab

4. Switch the "Basic" tab at the top of the build Settings page to "all"

5. Find the "Paths\header Search Paths" entry and add "/USR/INCLUDE/LIBXML2" to the list

6. Find the "Linking\other Linker Flags" entry and add "-LXML2" to the list

In the header of the code file that needs to call Gdataxml, add:

#import "GDataXMLNode.h"

Touchxml

1. Download the latest Touchxml source from Https://github.com/TouchCode/TouchXML

2. After extracting, drag the files and directories in the sources directory into the project

3. Select the project and select the "Build Settings" tab

4. Switch the "Basic" tab at the top of the build Settings page to "all"

5. Find the "Paths\header Search Paths" entry and add "/USR/INCLUDE/LIBXML2" to the list

6. Find the "Linking\other Linker Flags" entry and add "-LXML2" to the list

In the header of the code file that needs to call Touchxml, add:

#import "TouchXML.h"

TBXML

1. Download the latest Tbxml source from http://www.tbxml.co.uk/TBXML/TBXML_Free.html

2. After extracting, drag the TBXML.h, TBXML.M, NSDataAdditions.h and nsdataaddtions.m four files into the project

3. Add an application to the Libz.dylib class library

In the header of the code file that needs to call Tbxml, add:

#import "TBXML.h"

Instructions for useGdataxml

Read the XML file and parse it into a DOM Document object example:

NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "xml"];

NSData *xmldata = [[Nsmutabledata alloc] initwithcontentsoffile:filepath];

Nserror *error;

Gdataxmldocument *doc = [[Gdataxmldocument alloc] Initwithdata:xmldata options:0 error:&error];

if (doc = = nil) {return nil;}

NSLog (@ "%@", doc.rootelement);

[Doc release];

[XmlData release];

Introduction to various platforms for parsing XML ios-data

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.