Common parsing XML class libraries and brief installation methods in IOS development _ios

Source: Internet
Author: User
Tags xml parser xpath

In the iphone development, XML parsing has many options, the IOS SDK provides Nsxmlparser and libxml2 two class libraries, as well as a number of Third-party class libraries to choose from, such as Tbxml, Touchxml, Kissxml, TinyXML and Gdataxml. The question is which one should you choose?
There are usually two ways to parse XML, DOM and SAX:
When the DOM parses XML, it reads the entire XML document and constructs a tree structure (the node tree) that resides in memory, by traversing the tree structure to retrieve any XML node and read its properties and values. In general, you can query XML nodes directly by using XPath.
Sax parsing XML, is based on event notification mode, while reading XML document processing, do not have to wait for the entire document after loading to take action, when the read parsing process to encounter the object that needs to be processed, will issue a notification to process it.
Generally in the iOS platform, the comparison of common 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, this is a sax-style parsing XML class library, which is included in the iOS SDK by default and is simpler to use.
libxml2,http://xmlsoft.org/, a set of Open-source libraries that are included in the iOS SDK by default, is based on the C language API, so it may not be as easy to use as Nsxml. This set of class libraries supports both DOM and sax parsing, LIBXML2 's sax parsing is still very cool, because it can read side parsing, especially in the download from the Internet a large XML file, you can download side of the content has been downloaded to resolve, greatly improve the efficiency of the resolution.
Tbxml,http://www.tbxml.co.uk/tbxml/tbxml_ Free.html, this is a lightweight DOM-style XML parsing class library with good performance and low memory footprint, but it does not validate XML formats, does not support XPath, and supports parsing only, and does not support the modification of XML.
Touchxml,https://github.com/touchcode/touchxml, this is also a DOM-style XML parsing class library that supports XPath and does not support XML modification.
Kissxml,http://code.google.com/p/kissxml/, which is a set of XML parsing classes based on Touchxml, supports XML modification compared to Touchxml.
Tinyxml,http://www.grinninglizard.com/tinyxml/, this is a small set of C-language DOM for XML parsing of the class library, support for XML read and modify, do not directly support XPath, You need 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 Google-developed DOM-style XML parsing class library that supports reading and modifying XML documents, and supports XPath-style queries.
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, you can 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 text, basically more accurate and objective, the article proposes:
If you are reading very small XML documents, performance is basically no different, but from the convenience of the call, it is recommended to use Touchxml, Kissxml, or Gdataxml
If you need to read and modify XML documents, it is recommended that you use Kissxml or Gdataxml
If you need to read very large XML documents, it is recommended that you use LIBXML2 or Tbxml
If you don't want to call a Third-party class library, then using Nsxml can also


Installation Instructions
Gdataxml
1. Download "Gdata-objective-c client library" from Http://code.google.com/p/gdata-objectivec-client/downloads/list.
2. Extract the files, locate the Source\xmlsupport, and drag the GDataXMLNode.h and GDATAXMLNODE.M files to the project
3. Select the item, select the Build Settings tab page
4. Switch the "Basic" tab at the top to "all" in the Build Settings page
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






In need of calling Gdataxml code file header, add:

 code as follows:

#import "GDataXMLNode.h"





Touchxml
1. Download the latest Touchxml source from Https://github.com/TouchCode/TouchXML
2. After decompression, the files and directories in the sources directory are dragged to the project
3. Select the item, select the Build Settings tab page
4. Switch the "Basic" tab at the top to "all" in the Build Settings page
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
In need of calling Touchxml code file header, add:


 code as follows:
#import "TouchXML.h"

Tbxml
1. Download the latest Tbxml source from http://www.tbxml.co.uk/TBXML/TBXML_Free.html
2. After decompression, drag the TBXML.h, TBXML.M, NSDataAdditions.h and nsdataaddtions.m four files into the project
3. Add to the application of the Libz.dylib class library
In need of calling Tbxml code file header, add:
 code as follows:
#import "TBXML.h"






Instructions for use
Gdataxml
read the XML file and parse it as a DOM Document object example:


 code as follows:

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.