Install and use libxml2 in Ubuntu 14.04

Source: Internet
Author: User

For details about how to view libxml2 library, refer:

How to install libxml2 library in Ubuntu 14.04:

Sudo apt-get install libxml2

Sudo apt-get install libxml2-dev

After installing the software package using the preceding command, you can use the following command to check the status of the software package (determine whether the installation is successful)

Libxml2-dev dpkg-s

You can also run the following command (the result is the same ):

Dpkg-query-s libxml2-dev

After the software is installed, the header files of libxml2 are stored in the/usr/include/libxml2/libxml directory by default, this information can be viewed using the following two commands (any command can view the relevant information)

Dpkg-L libxml2-dev

Xml2-config -- cflags

Libxml2 library files are placed in the/usr/lib/i386-linux-gnu Directory, which can be viewed using the following two commands (any command can view the relevant information)

Dpkg-L libxml2-dev

Xml2-config -- libs

The corresponding libxml2 has a tool named xml2-config, the directory where the xml2-config is located is/usr/bin, in fact this is a shell script, detailed information about the xml2-config can be viewed through the following command

Man xml2-config

After the libxml2 library is installed, you can start development based on the libxml2 library. Currently, the following program is named CreateXmlFile. c, which is developed based on the libxml2 library.

Recommended reading:

Debian software package management tools apt-get and dpkg operation memo

Debian kernel compilation err: line 65 dpkg-gencontrol command not found

The Ubuntu Installation Software prompts "the lock file cannot be opened/var/lib/dpkg/lock ".

Common Parameters for installing Ubuntu dpkg Software

Linux-dpkg software package and APT software package management operations

Apt-get | dpkg | about YUM usage (English)

The Code is as follows:

/**********************************
Created: 2014/05/12
Filename: CreateXmlFile. c
Auther: wang kai
Depend: libxml2.lib
Purpose: Create an xml file
**********************************/

# Include <stdio. h>
# Include <libxml/parser. h>
# Include <libxml/tree. h>

Int main (int argc, char ** argv)
{
// Define document pointer
XmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0 ");

// Define node pointer
XmlNodePtr root_node = xmlNewNode (NULL, BAD_CAST "root ");

// Set the root element of the document
XmlDocSetRootElement (doc, root_node );

// Create child nodes directly in the root node
XmlNewTextChild (root_node, NULL, BAD_CAST "newnode1", BAD_CAST "newnode1 content ");
XmlNewTextChild (root_node, NULL, BAD_CAST "newnode2", BAD_CAST "newnode2 content ");

// Create a new node
XmlNodePtr node = xmlNewNode (NULL, BAD_CAST "node2 ");
// Create a new text node
XmlNodePtr content = xmlNewText (BAD_CAST "node content ");

// Add a new node to parent
XmlAddChild (root_node, node );
XmlAddChild (node, content );
// Create a new property carried by a node
XmlNewProp (node, BAD_CAST "attribute", BAD_CAST "yes ");

// Create a son and grandson node element
Node = xmlNewNode (NULL, BAD_CAST "son ");
XmlAddChild (root_node, node );
XmlNodePtr grandson = xmlNewNode (NULL, BAD_CAST "grandson ");
XmlAddChild (node, grandson );
XmlAddChild (grandson, xmlNewText (BAD_CAST "THis is a grandson node "));
 
// Dump an XML document to a file
Int nRel = xmlSaveFile ("CreatedXml. xml", doc );
If (nRel! =-1)
Printf ("an xml document was created and written to % d bytes \ n", nRel );
 
// Free up all the structures used by a document, tree Encoded DED
XmlFreeDoc (doc );
// Printf ("Hello World! \ N ");
Return 0;
}

The following command can be used to compile this program:

Gcc-I/usr/include/libxml2 CreateXmlFile. c-o CreateXmlFile-L/usr/lib/i386-linux-gnu-lxml2

Here, the-I parameter is used to specify the path of the gcc compiler to find the header file, the-L parameter is used to specify the path of the libxml2 library file, and the last-lxml2 specifies the specific library file. (-Lxml2 must be placed at the end of the command, otherwise the error of finding the Link Library will occur, as shown in)

I have not figured out the specific reason why I must put-lxml2 at the end. Further research is required.

The compilation command can also be written as follows:

Gcc 'xml2-config -- cflags '-L/usr/lib/i386-linux-gnu CreateXmlFile. c-o CreateXmlFile-lxml2

Or

Gcc 'xml2-config -- cflags 'CreateXmlFile. c-o CreateXmlFile-L/usr/lib/i386-linux-gnu-lxml2

Or

Gcc CreateXmlFile. c-o CreateXmlFile 'xml2-config -- cflags -- libs'

Although the form is different, the actual content of the command is the same. Because the command xml2-config -- the execution result of cflags is

-I/usr/include/libxml2 (specify the directory of the include header file)

Command xml2-config -- libs execution result is

-L/usr/lib/i386-linux-gnu-lxml2 (specifies the directory where libxml2 library files are located and the specific library files)

(Whatever the form, you only need to ensure that-lxml2 is at the end of the compiled command)

Upgrade Linux built-in libxml2 library install php-5.2.5 on RedHat Linux

Ubuntu libxml2

Use of Libxml2 in Linux

Use arm-none-linux-gnueabi in Ubuntu to cross-compile libxml2

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.