Xml c libxml class library usage

Source: Internet
Author: User

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 >

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.