Linux (Ubuntu10.04) libxml2 installation and use example

Source: Internet
Author: User

Libxml2 installation:

1. Installation Package: http://xmlsoft.org/where I download: http://xmlsoft.org/sources/old/

2. Download the compressed package, decompress it, unzip the command is: sudo tar xvzf libxml2-2.7.1.tar.gz

3. Configure and compile the installation process:

After decompression, go to the extracted Folder:

Cd libxml2-2.7.1

(Installed in the default path)

Sudo./configure

Sudo make

Sudo make install

(Set the installation path by yourself)

Or

Sudo./configure -- prefix/home/user/myxml/xmlinst

Sudo make

Sudo make install

Export PATH =/home/user/myxml/xmlinst/bin: $ PATH

 

I installed it in the default path, so the following sample compilation method is for the default path.

Typical libxml2 application example xmlCreator. cpp:

 1 #include <stdio.h> 2 #include <libxml/parser.h> 3 #include <libxml/tree.h> 4  5 int main(int argc,char **argv) 6 { 7     xmlDocPtr doc = NULL;/*document pointer*/ 8     xmlNodePtr root_node = NULL,node = NULL,node1 = NULL;/*node pointers*/ 9     //Creates a new document,a node and set it as a root node10     doc = xmlNewDoc(BAD_CAST"1.0");11     root_node = xmlNewNode(NULL,BAD_CAST"root");12     xmlDocSetRootElement(doc,root_node);13     //creates a new node,which is "attached" as child node of root_node node.14     xmlNewChild(root_node,NULL,BAD_CAST"node1",BAD_CAST"content of node1");15     16     //xmlNewProp()creates attributes,which is "attached" to an node.17     node = xmlNewChild(root_node,NULL,BAD_CAST"node3",BAD_CAST"node has attributes");18     xmlNewProp(node,BAD_CAST"attribute",BAD_CAST"yes");19     20     //Here goes another way to create nodes.21     node = xmlNewNode(NULL,BAD_CAST"node4");22     node1 = xmlNewText(BAD_CAST"other way to create content");23     24     xmlAddChild(node,node1);25     xmlAddChild(root_node,node);26 27     //Dumping document to stdio or file28     xmlSaveFormatFileEnc(argc > 1 ? argv[1]:"-",doc,"UTF-8",1);29 30     /*free the document*/31     xmlFreeDoc(doc);32     xmlCleanupParser();33     xmlMemoryDump();//debug memory for regression tests34     35     return 0;36 }

Compilation process: This is the cpp file. Therefore,G ++ xmlCreator. cpp-o xmlCreator-I/usr/local/include/libxml2-L/usr/local/lib-lxml2

If it is a c file, it should be changed:Gcc xmlCreator. cpp-o xmlCreator-I/usr/local/include/libxml2-L/usr/local/lib-Lxml2

* *****-I directory of the Post-connector file-L directory followed by the lib Library

 

......$ gcc xmlCreator.cpp -o xmlCreator -I /usr/local/include/libxml2 -lxml2/tmp/ccPeKBRE.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status
......$ g++ xmlCreator.cpp -o xmlCreator -I /usr/local/include/libxml2 -lxml2        ---------------ok

 

 Therefore, when this error occurs, check whether the compiler is using an error. Use gcc to compile the C program, and use g ++ to compile the C ++ program. The check mark is no problem.

After the xmlCreator file is generated, the running result is as follows:

1 <?xml version="1.0" encoding="UTF-8"?>2 <root>3   <node1>content of node1</node1>4   <node3 attribute="yes">node has attributes</node3>5   <node4>other way to create content</node4>6 </root>

At this point, a simple example of creating an xml file using the libxml2 library is complete ......

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.