XML
Reference address:
Http://jianlee.ylinux.org/Computer/C/libxml.html
Instance:
/*
* ===================================================== ========================================================== ======
*
* Filename: XML. c
*
* Description:
*
* Version: 1.0
* Created: 07/07/2011 11:47:54 AM
* Revision: None
* Compiler: gcc
*
* Author: kangle. Wang (Mn), wangkangluo1@gmail.com
* Company: APE-TECH
*
* ===================================================== ========================================================== ======
*/
# Include < Stdio. h >
# Include < Stdlib. h >
# Include < Libxml / Parser. h >
# Include < Libxml / Tree. h >
Int Main ( Int Argc, Char ** Argv)
{
Xmldocptr pdoc = NULL;
Xmlnodeptr proot_node = Null, pnode = Null, pnode1 = NULL;
// Create a new document and set the root node
// An XML file has only one root node.
Pdoc = Xmlnewdoc (bad_cast " 1.0 " );
Proot_node = Xmlnewnode (null, bad_cast " Root Node " );
Xmlnewprop (proot_node, bad_cast " Version " , Bad_cast " 1.0 " );
Xmldocsetrootelement (pdoc, proot_node );
Pnode = Xmlnewnode (null, bad_cast " Subnode 1 " );
// Create the child node of the above pnode
Xmlnewchild (pnode, null, bad_cast " Subnode 1 " , Bad_cast " Information " );
// Add a subnode to the root node
Xmladdchild (proot_node, pnode );
Pnode1 = Xmlnewnode (null, bad_cast " Subnode 1 " );
Xmladdchild (pnode, pnode1 );
Xmladdchild (pnode1, xmlnewtext (bad_cast " This is a lower node, subnode 1 " ));
// You can also directly create a subnode to the root node.
Xmlnewtextchild (proot_node, null, bad_cast " Subnode 2 " , Bad_cast " Contents of subnode 2 " );
Xmlnewtextchild (proot_node, null, bad_cast " Subnode 3 " , Bad_cast " Contents of subnode 3 " );
// Save XML as a file. If no file name parameter is provided, it is output to the standard output.
Xmlsaveformatfileenc (argc > 1 ? Argv [ 1 ]: " - " , Pdoc, " UTF-8 " , 1 );
// Release resources
Xmlfreedoc (pdoc );
Xmlcleanupparser ();
Xmlmemorydump ();
Return 0 ;
}
Compile: gcc-g-wall-O0 XML. C-o XML-I/usr/include/libxml2-lxml2
Run:./XML test. xml
Result:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Root Node version = "1.0 " >
< Subnode 1 >
< Subnode 1 > Information </ Subnode 1 >
< Subnode 1 > This is a lower node, subnode 1 </ Subnode 1 >
</ Subnode 1 >
< Subnode 2 > Contents of subnode 2 </ Subnode 2 >
< Subnode 3 > Contents of subnode 3 </ Subnode 3 >
</ Root Node >