Linux use under libxml2

 

 

http://www.cnblogs.com/flysnail/archive/2012/06/25/2561319.html

----

First, download, install

Download libxml2, the official website, download the appropriate own version of the package.

http://rpmfind.net/linux/rpm2html/search.php?query=libxml2-devel

I downloaded version libxml2-devel-2.6.32-11.10.i586

Decompress, compile and run

./configure -prefix / usr / local / xml # installation directory

make

make install

After installation / under usr / local / xml generates four will be more folders

bin  include  lib  share

Second, run the demo

Use Eclipse development, the new C ++ project.

1, the first document to solve the routing problem.

The default directory contains libxml will automatically add the header files. If Eclipse does not automatically add, we can manually add, right properties, make the following changes

Header file path after I installed

$/include/libxml2/libxml/*.h

And a source code inside working directory defaults to <libxml / *. H>

This time the project is unable to find these files, we need to do is modify the directory.

$/include/libxml/*.h

Of course, there is a way that the code contains the header files changed a bit. E.g:

?
#include < libxml/xmlversion.h >

change into

#include < libxml2/libxml/xmlversion.h >

This is the way you want to dozens of header files do this deal, I just do not be so stupid.

2、demo src

Copy the code
void   writeXML ( void ) { 

    // definition documents and node pointer 
      xmlDocPtr DOC = xmlNewDoc (bad_cast " 1.0 " ); 
      xmlNodePtr to root_node = xmlNewNode (NULL, bad_cast " the root " );
       // set the root node 
      xmlDocSetRootElement (DOC, root_node);
       / / created 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 " ); 
      xmlNewTextChild (root_node, NULL, bad_cast " newNode3 " , bad_cast " newNode3 Content " );
       // Create a node, setting its content and properties, and then added to the root node 
      xmlNodePtr to Node = xmlNewNode (NULL, bad_cast " node2 " ); 
      xmlNodePtr to Content = xmlNewText (bad_cast " NODEs the CONTENT " ); 
      xmlAddChild (root_node, Node); 
      xmlAddChild (Node, Content); 
      xmlNewProp (Node, bad_cast " attribute " , bad_cast "yes");
       // create a son and grandson nodes 
      the Node = xmlNewNode (NULL, bad_cast " Son " ); 
      xmlAddChild (root_node, the Node); 
      xmlNodePtr Grandson = xmlNewNode (NULL, bad_cast " Grandson " ); 
      xmlAddChild (the Node, Grandson); 
      xmlAddChild (Grandson, xmlNewText (bad_cast " This IS A Grandson the Node " ));
       // store xml document 
      int NREL = 0 ; 
      NREL = xmlSaveFile ( " CreatedXml.xml " , DOC);
       IF (NREL = -! 1) 
      { 
         COUT << " an xml document is created, written " << << NREL " bytes " << endl; 
      } 
      // release dynamic application node within the document memory 
      xmlFreeDoc (DOC); 
}
Copy the code

3, set lib

Eclipse Set link path: / usr / local / xml / lib

Set link library xml2

Use the command compile time, add the following parameters:

-L /usr/local/xml/lib / -lxml2

Well, the following compile and run ok.



Category: Linux Develop

Reproduced in: https: //www.cnblogs.com/kungfupanda/archive/2012/08/28/2660702.html

Guess you like

Origin blog.csdn.net/weixin_33892359/article/details/94494264