Comparison and installation of XML parsing class libraries on iOS platform

Source: Internet
Author: User
Tags xml parser

In iPhone development, there are many options for XML parsing. the ios sdk provides two class libraries: nsxmlparser and libxml2. There are also many third-party class libraries available, such as tbxml, touchxml, kissxml, tinyxml, and gdataxml. Which one should be selected?

There are two ways to parse XML: Dom and sax:

  • When Dom parses XML, it reads the entire XML document and constructs a tree (node tree) with resident memory. By traversing the tree structure, it can retrieve any XML node and read its attributes and values. In addition, you can use XPath to directly query XML nodes.
  • Sax parses XML, which is based on the Event Notification mode. It reads and processes XML documents without waiting for the entire document to be loaded, when an object to be processed is encountered during the read and resolution process, a notification is sent to process the object.

Generally, common XML parsing class libraries on iOS platforms include the following types:

  • Nsxmlparser, http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html, a class library that parses XML in a sax way, which is included in the ios sdk by default and easy to use.
  • Libxml2, http://xmlsoft.org/, is a set of open-source class libraries included in the ios sdk. It is based on the C language API, so it may not be as convenient to use as nsxml. This class library supports both Dom and sax parsing. the libxml2's sax parsing method is cool because it can read and parse, especially when downloading a large XML file from the Internet, you can parse the downloaded content while downloading, greatly improving the resolution efficiency.
  • Tbxml, batch.
  • Touchxml, struct.
  • Kissxml, metadata.
  • Tinyxml, struct.
  • Gdataxml, metadata.
  • So how to select the appropriate XML parsing class library in the project? Some people on the Internet have made analysis and comparison of these XML class libraries, can refer to the "how to choose the best XML Parser for your iPhone project" http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project article, basically more accurate and objective, suggestions:
  • For reading small XML documents, there is basically no performance difference. However, we recommend that you use touchxml, kissxml, or gdataxml for ease of calling.
  • To read and modify XML documents, we recommend that you use kissxml or gdataxml
  • If you need to read a very large XML document, we recommend that you use libxml2 or tbxml
  • If you do not want to call a third-party class library, you can also use nsxml.
Installation instructions: gdataxml

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

2. Extract the file, find source \ xmlsupport, and drag the gdataxmlnode. h and gdataxmlnode. M files to the project.

3. Select a project and select the "build Settings" tab.

4. Switch the "Basic" label on the top of the build settings page to "all"

5. Locate the "paths \ header search paths" item and add "/usr/include/libxml2" to the list.

6. Locate the "linking \ other linker flags" item and add "-lxml2" to the list.

Add:

# Import "gdataxmlnode. H"

Touchxml

1. download the latest touchxmlsource code from https://github.com/touchcode/touchxml

2. decompress the package and drag the files and directories under the sources directory to the project.

3. Select a project and select the "build Settings" tab.

4. Switch the "Basic" label on the top of the build settings page to "all"

5. Locate the "paths \ header search paths" item and add "/usr/include/libxml2" to the list.

6. Locate the "linking \ other linker flags" item and add "-lxml2" to the list.

Add:

# Import "touchxml. H"

Tbxml

1. download the latest tbxmlsource code from http://www.tbxml.co.uk/tbxml/tbxml_free.html

2. decompress the package and drag the tbxml. H, tbxml. M, nsdataadditions. H, and nsdataaddtions. M files to the project.

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

Add:

# Import "tbxml. H"

Gdataxml

Example of reading an XML file and parsing it as a DOM Document Object:

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];

Related Article

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.