IOSPlatformXML ParsingThe comparison and installation of class libraries are described in this article. In iPhone development,XMLOfAnalysisThere are many options,IOSThe 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?
Parse XMLThere are usually two methods: DOM and SAX:
When DOM parses XML, it reads the entire XML document and constructs a TREE 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, which is 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, Bytes.
TouchXML, Bytes.
KissXML, Bytes.
TinyXML, Bytes.
GDataXML, Bytes.
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”
Instructions for use
GDataXML
ReadXMLFile, andAnalysisExample of becoming 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];
Summary:IOSPlatformXML ParsingThe comparison and installation instructions of the class libraries are complete. I hope this article will help you!