Blogger: Yi Feifei Link: http://www.yifeiyang.net/iphone-web-development-techniques-of-the-chapter-1-parsing-xml/ Reprinted text.
IPhone XML library
XML parsing and memory usage
Libxml2 vs nsxmlparser
-
Nsxmlparser example
-
Libxml2 example
-
-
Item: Add libxml
-
The SAX Parser in libxml
Use Dom to parse
-
Google data APIs
-
Touchxml
-
Kissxml
IPhone development skills network (1) --- parsing XML Develop network applications on the iPhoneProgramXML documents, such as soap, rest, and RSS in Web applications, are often parsed based on XML. It is very important to master the XML parsing technology. Here I will introduce several methods for parsing XML under the iPhone and compare their performance. IPhone XML libraryThere are two standard XML parsing libraries in the iPhone, libxml2 and nsxmlparser. Libxml2 was developed by the gnome project. Because it is an MIT open protocol, it has been transplanted to many platforms and can also be used on the iPhone. Libxml2 features a fast speed. In addition, as the most basic XML parser, it provides Sax and Dom parsing. It corresponds to the most XML standards, such as namespace, XPath, xpointer, HTML, xinclude, XSLT, XML schema, and relax ng. In addition, it is written in C language and is relatively fast. Nsxmlparser is the XML Parser contained in cocoa. It only provides the function of sax parsing. Because it is part of cocoa and the API is objective-C, it is highly compatible with MAC systems and is relatively simple to use. XML parsing and memory usage Because the iPhone is also an embedded device, like other embedded devices, it also has problems with memory usage, CPU usage, and other resources. Therefore, when selecting the code repository, you need to consider the performance and memory usage issues. Generally, the XML Parser has two methods: sax parsing and Dom parsing. In contrast, the SAX Parser is relatively compact and lean, and consumes less memory. This is because its design philosophy is completely different from that of Dom. The data is parsed while the data is received, and the data is notified by callback. The concept of DOM tree is absent. Currently, the ram of the iPhone 3G is 128 MB (3 GS is 256 MB ). IPhone OS is used, and resident programs such as MP3, email, and Safari are used in different scenarios. Basically, the memory size available for your program is about 10 MB. When developing an XML parser, you must note that the XML files are generally relatively large. If Dom is used, the amount of memory consumed must be large. Therefore, in the iPhone, the two Resolvers can only use the sax parsing method. The DOM method can only be used on the simulator (such as the nsxmldocument class). It is not used on the actual device. (However, I will introduce a third-party method to use DOM in the following sections, which is worth using for small XML documents .) Libxml2 vs nsxmlparser Generally, the libxml2 SAX Parser is used, or nsxmlparser is used. We will use the attached example xmlperformance In the SDK below for a test. The same XML document is downloaded from the internet and parsed. The comparison result is as follows:
|
Download time |
Resolution time |
Total |
Nsxmlparser |
1.419 s |
5.525 s |
7.134 s |
Libxml2 |
2.520 s |
2.247 s |
2.646 s |
We can see that libxml2 is much faster than nsxmlparser. This has some relationship with their processing methods. when calling the sax API in nsxmlparser, parameters are transmitted as strings and must be converted to nsstring or nsdictionary objects first, in addition, it does not parse the file while downloading the libxml2 file. The parsing starts only after the entire file is downloaded. Therefore, libxml2 is recommended. Nsxmlparser exampleParsed XMLCodeExample:
1 2 3 4 5 |
<? XML version ="1.0"Encoding =UTF-8"?> <Users> <User name ="Hoge"Age ="20"/> <User name ="Fuga"Age ="30"/> </Users> |
The Code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Static Nsstring * Feedurlstring = @ Http://www.yifeiyang.net/test/test.xml" ;
-( Void ) Parserdidstartdocument Nsxmlparser *) Parser { // Processing at the beginning of resolution }
-( Void ) Parsexmlfileaturl Nsurl *) URL Parseerror Nserror **) Error { Nsxmlparser * Parser = [[ Nsxmlparser Alloc] initwithcontentsofurl: url]; [Parser setdelegate: Self ]; [Parser setshouldprocessnamespaces: No ]; [Parser setshouldreportnamespaceprefixes: No ]; [Parser setshouldresolveexternalentities: No ]; [Parser parse]; Nserror * Parseerror = [Parser parsererror]; If (Parseerror & error ){ * Error = parseerror; } [Parser release]; }
-( Void ) Parser Nsxmlparser *) Parser Didstartelement Nsstring *) Elementname Namespaceuri Nsstring *) Namespaceuri Qualifiedname Nsstring *) QNAME Attributes Nsdictionary *) Attributedict { // Element start handle If (QNAME ){ Elementname = QNAME; } If ([Elementname isequaltostring :@ "User" ]) { // Output property value Nslog (@ "Name Is % @, age is % @" , [Attributedict objectforkey :@ "Name" ], [Attributedict objectforkey :@ "Age" ]); } }
-( Void ) Parser Nsxmlparser *) Parser Didendelement Nsstring *) Elementname Namespaceuri Nsstring *) Namespaceuri Qualifiedname Nsstring *) QNAME { // Element end handle If (QNAME ){ Elementname = QNAME; } }
-( Void ) Parser Nsxmlparser *) Parser Foundcharacters Nsstring *) String { // Retrieve the text of an element }
Nserror * Parseerror = Nil ; [ Self Parsexmlfileaturl: [nsurl urlwithstring: feedurlstring] parseerror: & parseerror]; |
In actual use, except the last two lines, the last two of all such as a class are the code that starts the class. |