Use of Libxml2 in Linux

Source: Internet
Author: User

1. Download and install LIBXML2
Method 1]

Libxml2 is a C-language XML library that provides simple and convenient operations on XML files, and supports XPATH query and some XSLT conversion functions. Libxml2 is the http://xmlsoft.org/
The full version of the library is open-source with examples and instructions. I developed the C language in linux, and downloaded the source code package of libxml2-2.6.tar.gz.

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

Ubuntu libxml2

Installation steps:
1. Extract: $ tar zxvf libxml2-2.6.20.tar.gz
2, enter the installation directory after decompression: $ cd libxml2-2.6.20
3. Install trilogy: 1) $./configure
2) $ make
3) $ make install
Installation is complete.

Libxml2 installation error:

Cannot remove 'libtoolt': No such file or directory

Solution:

Modify the configure file

$ Vim configure

Delete this row: $ RM "$ deleted file"

Run $./configure again.

 

When you install libxml2,./configure -- prefix =/usr/local
After installation, You need to redefine:
Export LD_LIBRARY_PATH =/usr/local/lib
Export PKG_CONFIG_PATH =/usr/local/lib/pkgconfig
The specific location depends on the path specified by the prefix.

 

Method 2]

# Sudo apt-get install libxml2

# Sudo apt-get install libxml2-dev

 

Method 3]

Libxml2 installation and use favorites

You need to save and exchange data in the project, in order to facilitate the use of common text to save. However, for text parsing, considering the robustness of the program, it is difficult to handle many unexpected situations. Therefore, I am interested in xml. Xml itself has a lot of information on the website and can be referenced at any time.

 

The Gnome project provides an xml C Parser: Libxml2. Libxml2 provides the latest xmlreader interface, which is easy to use, but is supported only in Versions later than 2.6. Today, I made a try. I did a simple test and I was mixed up with my own small program.

 

The latest version is 2.6.30, which can be downloaded from the http://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6.

 

It's easy to install.

// Install

#./Configure

# Make

# Make install

// Test install

# Make tests

// Uninstall

# Make uninstall

 

If you do not need to customize it, You can directly default it in the configure stage. Libxml2 will install the header file and library in the/usr/local/include/libxml2/libxml directory by default. Therefore, it may cause you to encounter the "no such file" header file error when compiling your program for the first time (for example, I am so stupid when writing ).

 

Libxml2 provides a solution that provides you with three convenient tools, xml2-config, xmlcatalog, and xmllint, In the/usr/local/bin directory: in fact, it is not convenient at all. I want to explain how good it is to someone in advance. I have to ask someone to check the information ). The xml2-config is used in compilation.

 

Xml2-config can... Let's take a look at it.

[Root @ Amanda ~] # Xml2-config

Usage: xml2-config [OPTION]

Known values for OPTION are:

-- Prefix = DIR change libxml prefix [default/usr/local]

-- Exec-prefix = DIR change libxml exec prefix [default/usr/local]

-- Libs print library linking information

-- Cflags print pre-processor and compiler flags

-- Modules module support enabled

-- Help display this help and exit

-- Version output version information

 

 

Here we will talk about the parameters used for compilation: -- cflags and -- libs. The help shows that this provides assistance for project compilation. They provide the following information:

 

[Root @ Amanda ~] # Xml2-config -- cflags

-I/usr/local/include/libxml2

[Root @ Amanda ~] # Xml2-config -- libs

-L/usr/local/lib-lxml2-lz-lm

 

So when compiling your own test program, you can directly use # gcc-I/usr/local/include/libxml2-L/usr/local/lib-lxml2-lz-lm-o test. c.

 

Or add it to makefile, for example:

CFLAGS = 'xml2-config -- cflags'

LIBS = 'xml2-config -- libs'

 

Test procedure:

# Include <stdio. h>

# Include <libxml/parser. h>

# Include <libxml/tree. h>

Int main (int argc, char ** argv)

{

XmlDocPtr doc = NULL;

XmlNodePtr root_node = NULL, node = NULL, node1 = NULL;

Doc = xmlNewDoc (BAD_CAST "1.0 ");

Root_node = xmlNewNode (NULL, BAD_CAST "root ");

XmlDocSetRootElement (doc, root_node );

XmlNewChild (root_node, NULL, BAD_CAST "node1", BAD_CAST "content of node1 ");

Node = xmlNewChild (root_node, NULL, BAD_CAST "node3", BAD_CAST "node has attributes ");

XmlNewProp (node, BAD_CAST "attribute", BAD_CAST "yes ");

Node = xmlNewNode (NULL, BAD_CAST "node4 ");

Node1 = xmlNewText (BAD_CAST "other way to create content ");

XmlAddChild (node, node1 );

XmlAddChild (root_node, node );

XmlSaveFormatFileEnc (argc> 1? Argv [1]: "-", doc, "UTF-8", 1 );

XmlFreeDoc (doc );

XmlCleanupParser ();

XmlMemoryDump ();

Return (0 );

}

 

Use the following command for compilation.

Gcc-I/usr/local/include/libxml2-L/usr/local/lib-lxml2 test. c-o test

  • 1
  • 2
  • 3
  • 4
  • Next Page

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.