Using libxml2 serialization (2 usage overview and XML Tree Generation and resolution)

Source: Internet
Author: User
Tags xml parser
Introduction

Data Type:Xmlchar replaces char, a one-byte string encoded by a UTF-8. If your data uses other encoding, it must be converted to the UTF-8 before using libxml functions. Xmldoc contains the tree structure created by the parsing document. xmldocptr is a pointer to this structure. Xmlnodeptr and xmlnode contain a single node structure. xmlnodeptr is a pointer to this structure and is used to traverse the document tree. Advantages: 1. installation and use are relatively simple and easy to get started; 2. many supported encoding formats can solve Chinese problems (using a very simple encoding conversion function); 3. support for xpath parsing (this is useful for any location of nodes in the XML document); 4. supports well-formed and valid verification. Specifically, it supports DTD verification. The schema verification function is being improved. (currently, most Resolvers do not fully support Shema verification. supports common Dom and sax parsing methods. Deficiency: 1. there are too many pointers, and errors may occur when you are not using them. Common segment errors are displayed in Linux systems, and improper management may easily cause memory leakage. 2. I personally think that some functions in the interior are not well designed (for example, to obtain an XPATH function, it does not obtain node attributes, so in some cases it will not be located ). The best learning manual for libxml2 is the development manual provided by official developers. Libxml2-devel-2.6.19, Rpm-Q-D libxml2Obtain the document path. About XMLBefore studying the libxml2 library, let's first consolidate the XML-related basics. XML is a text-based format that can be used to create structured data that can be accessed through various languages and platforms. It includes a series of HTML-like tags and arranges them in a tree structure. For example, see the simple document in Listing 1. This is a simplified version of the sample configuration file studied in the configuration file section. To better display the general concepts of XML, we simplified it.
List 1.A simpleXMLFile
 
<?xml version="1.0" encoding="UTF-8"?>
<files>
 <owner>root</owner>
 <action>delete</action>
 <age units="days">10</age>
</files>
The first line in Listing 1 is the XML declaration, which tells the application responsible for processing XML, that is, the parser, the version of the XML to be processed. Most of the files are written in Version 1.0, but there are also a small number of files in version 1.1. It also defines the encoding used. Most files use UTF-8, but XML is designed to integrate data in a variety of languages, including those that do not use English letters. Next we will see elements. An element Start markStart (such as <files>) and End markEnd (for example, </Files>), where the slash (/) is used to distinguish it from the start mark. Element is Node. The XML Document Object Model (DOM) defines several different NodesType, including Elements(For example filesOr age), Attributes(For example units) And text (such rootOr 10). The element can have subnodes. For example, the age element has a child element, that is, a text node. 10. The files element has seven child elements. Three of them are obvious. They are three child elements: owner, actionAnd age. The other four are the blank text symbols before and after the elements. The XML Parser can use this parent-child structure to traverse documents or even modify the structure or content of documents. Libxml2 is one of these Resolvers, and the sample application in this article uses this structure to achieve this purpose. There are many different parser and libraries for different environments. Libxml2 is the best parser and library for UNIX environments. It has been extended and provides support for several scripting languages, such as Perl and python. 1 tree/*************************************** ***** Compile: gcc-I/usr/include/libxml2/-lxml2 tree1.c * usage: create a XML tree ************************************ * *******/# include <stdio. h> # include <libxml/parser. h> # include <libxml/tree. h> int main (INT argc, char ** argv) {xmldocptr Doc = NULL;/* Document pointer */xmlnodeptr root_node = NULL, node = NULL, node1 = NULL; /* node pointers * // creat Es a new document, a node and set it as a root node Doc = xmlnewdoc (bad_cast "1.0"); root_node = xmlnewnode (null, bad_cast "root"); xmldocsetrootelement (Doc, root_node); // creates a new node, which is "attached" as child node of root_node node. xmlnewchild (root_node, null, bad_cast "node1", bad_cast "content of node1"); // xmlnewprop () creates attributes, which is "attached" to an node. node = xmlnew Child (root_node, null, bad_cast "node3", bad_cast "node has attributes"); xmlnewprop (node, bad_cast "attribute", bad_cast "yes "); // here goes another way to create nodes. node = xmlnewnode (null, bad_cast "node4"); node1 = xmlnewtext (bad_cast "other way to create content"); xmladdchild (node, node1); xmladdchild (root_node, node ); // dumping document to stdio or file xmlsaveformatfileenc (argc> 1? Argv [1]: "-", Doc, "UTF-8", 1);/* free the document */xmlfreedoc (DOC); xmlcleanupparser (); xmlmemorydump (); // debug memory for regression tests return (0);} generated XML: [Denny @ localhost XML] $ gcc-I/usr/include/libxml2/-lxml2 tree1.c [Denny @ localhost XML] $. /. out <? XML version = "1.0" encoding = "UTF-8"?> <Root> <node1> content of node1 </node1> <node3 attribute = "yes"> node has attributes </node3> <node4> other way to create content </node4> </root> Execution sequence:1 declaration pointer: Document pointer (xmldocptr), node pointer (xmlnodeptr); 2 generate document DOC: xmlnewdoc3 generate root node root_node: xmlnewdocnode; xmlnewnode4 document is bound with the root node: xmldocsetrootelement5 node operation 1) create a subnode: xmlnewchild or xmlnewnode2) set the node attribute: xmlnewprop3) set the node value: xmlnewtext, xmlnewchild, xmladdchild6 release memory: xmlfreedoc, xmlmemorydump7 lib load exit: libxml_test_version, xmlcleanupparser 2 parseFor an application, the first step to read an XML file is to load the data and parse it into DocumentObject. On this basis, you can traverse the DOM tree to obtain specific nodes. /*************************************** ***** Compile: gcc-I/usr/include/libxml2/-lxml2 tree1.c * usage: tree2 filename_or_url ************************************** * *****/# include <stdio. h> # include <libxml/parser. h> # include <libxml/tree. h> # ifdef libxml_tree_enabled static voidprint_element_names (xmlnode * a_node) {xmlnode * cur_node = NULL; For (cur_node = a_node; cur_node = cur_node-> Next) {If (cur_node-> type = xml_element_node) {printf ("Node Type: element, name: % s/n", cur_node-> name );} print_element_names (cur_node-> Children) ;}/ *** simple example to parse a file called "file. XML ", * walk down the Dom, and print the name of the * XML elements nodes. */intmain (INT argc, char ** argv) {xmldoc * Doc = NULL; xmlnode * root_element = NULL; If (argc! = 2) Return (1); // libxml_test_version/* parse the file and get the Dom */DOC = xmlreadfile (argv [1], null, 0 ); if (Doc = NULL) {printf ("error: cocould not parse file % s/n", argv [1]);} /* Get the root element node */root_element = xmldocgetrootelement (DOC); print_element_names (root_element);/* free the document */xmlfreedoc (DOC); // xmlcleanupparser (); return 0 ;}# elseint main (void) {fprintf (stderr, "tree support not compiled in/N"); exit (1) ;}# endif Execution sequence:1 declaration pointer: Document pointer (xmldocptr), node pointer (xmlnodeptr); 2 get document DOC: xmlreadfile3 get root node root_node: xmldocgetrootelement4 node operation: 1) Get the node value: xmlnodegetcontent (corresponding to xmlfree) 2) traversal: pointing to the next node: xmlnodeptr-> Children node value: xmlnodeptr-> name, intra-node traversal: xmlnodeptr-> next5 release memory: xmlfreedoc, xmlfree

 

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.