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" >
- <root>
- <name value="WUSJ"/>
- <age>24</age>
- </root>
parsing this XML file
[CPP] view Plaincopy
- NSString *xmlpath = [[Nsbundlemainbundle] pathforresource:@"test" oftype:@"xml"];
- NSString *xmlstring = [Nsstringstringwithcontentsoffile:xmlpath Encoding:NSUTF8StringEncodingerror:nil];
- Gdataxmldocument *xmldoc = [[Gdataxmldocument alloc] initwithxmlstring:xmlstring options:0 Error:nil];
- Gdataxmlelement *xmlele = [xmldoc rootelement];
- Nsarray *array = [Xmlele children];
- NSLog (@"Count:%d", [array Count]);
- For (int i = 0; i < [array count]; i++) {
- Gdataxmlelement *ele = [array objectatindex:i];
- //According to the label name
- if ([[[Ele name] isequaltostring:@"name"]) {
- //Read the attribute inside the tag
- NSLog (@"name-to-%@", [[Ele attributeforname:@"value"] stringvalue]);
- } Else {
- //Direct-read string between tags
- NSLog (@ "age-and%@", [Ele stringvalue]);
- }
- }
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)