iOS learning: Common third-party libraries (Gdataxmlnode:xml parsing library)

Source: Internet
Author: User

 iOS Learning: Common third-party libraries (Gdataxmlnode:xml parsing library) 

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.

I. Description of Gdataxmlnode Gdataxmlnode is a set of classes provided by Google for XML data processing. This class set encapsulates libxml2--dom processing, reads and writes to smaller or medium XML documents, and supports XPath syntax.  How to use:1. Get the gdataxmlnode.h/m file and add the gdataxmlnode.h/m file to your project2. Adding "Libxml2.dylib" library to the project3. Find the "Header Search path" entry in the "Build Settings" page of the project and add/usr/include/libxml2 "to the path4, add "GDataXMLNode.h" file to the head file, if the project can be compiled through, then the Gdataxmlnode added successfully ii. Examples of Gdataxmlnode Example: [HTML] view plaincopy < param name= "wmode" value= "Transparent" >
    1. <root>
    2. <name value="WUSJ"/>
    3. <age>24</age>
    4. </root>
 parsing this XML file      [CPP] view Plaincopy
  1. NSString *xmlpath = [[Nsbundlemainbundle] pathforresource:@"test" oftype:@"xml"];
  2. NSString *xmlstring = [Nsstringstringwithcontentsoffile:xmlpath Encoding:NSUTF8StringEncodingerror:nil];
  3. Gdataxmldocument *xmldoc = [[Gdataxmldocument alloc] initwithxmlstring:xmlstring options:0 Error:nil];
  4. Gdataxmlelement *xmlele = [xmldoc rootelement];
  5. Nsarray *array = [Xmlele children];
  6. NSLog (@"Count:%d", [array Count]);
  7. For (int i = 0; i < [array count]; i++) {
  8. Gdataxmlelement *ele = [array objectatindex:i];
  9. //According to the label name
  10. if ([[[Ele name] isequaltostring:@"name"]) {
  11. //Read the attribute inside the tag
  12. NSLog (@"name-to-%@", [[Ele attributeforname:@"value"] stringvalue]);
  13. } Else {
  14. //Direct-read string between tags
  15. NSLog (@ "age-and%@", [Ele stringvalue]);
  16. }
  17. }

 Operation Result:             Summary of Gdataxmlnode methods The final data readout is read out in the Gdataxmlelement object, and the following methods are all methods of the Gdataxmlelement class1, name method, take tag name e.g name label2, Attributeforname: Take the attribute node StringValue can be taken to the property value e.g the Value property in the name tag3. StringValue: Take the string value between the tags e.g:age

DOM parsing

-(Ibaction) Dommethord: (ID) Sender {

1, get the file.

NSString *path = [[NSBundle mainbundle] pathforresource:@ "xml" oftype:@ "TXT"];

2, get the data according to the path

NSData *data = [NSData Datawithcontentsoffile:path];

3, creating a Parse object

Gdataxmldocument *document = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];

4, get the root

Gdataxmlelement *rootelement = document.rootelement;

Initializing an array

_alldatamutaarray = [Nsmutablearray array];

_alldatamutastring = [nsmutablestring string];

5, gets all child nodes under the root node

Nsarray *stuarrayelement = Rootelement.children;

6, traverse every student

For (Gdataxmlelement *stuelement in stuarrayelement) {

Create a model every time you iterate

Student *stu = [Student new];

7, traversing child nodes

For (Gdataxmlelement *stusubelement in Stuelement.children) {

8, copy the model object using the KVC method

[Stu SetValue:stuSubElement.stringValue ForKey:stuSubElement.name];

Print

NSLog (@ "%@%@", stusubelement.name,stusubelement.stringvalue);

[_alldatamutastring AppendString:stuSubElement.stringValue];

}

Put the added model into the array

[_alldatamutaarray Addobject:stu];

[Stu release];

NSLog (@ "----%lu", stu.retaincount);

}

NSLog (@ "Data item%@", _alldatamutaarray);

_textfieldview.text = _alldatamutastring;

[Document release];

}

iOS learning: Common third-party libraries (Gdataxmlnode:xml parsing library)

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.