Linux(Ubuntu10.04 )下libxml2的安裝以及使用樣本

來源:互聯網
上載者:User

libxml2的安裝:

1.安裝包:http://xmlsoft.org/,我下載的地方是http://xmlsoft.org/sources/old/

2.下載好壓縮包後,對其進行解壓,解壓的命令是:sudo tar xvzf libxml2-2.7.1.tar.gz

3.配置,編譯安裝過程:

解壓好之後,進入解壓好的檔案夾中:

cd libxml2-2.7.1

(預設路徑安裝)

sudo ./configure

sudo make

sudo make install

(自己設定安裝路徑)

或則

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

sudo make

sudo make install

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

 

我是按照預設路徑安裝的,因此下面的樣本編譯方法都是針對預設路徑的。

libxml2經典應用樣本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 }

編譯過程:此處是cpp檔案,因此是,g++ xmlCreator.cpp -o xmlCreator -I /usr/local/include/libxml2 -L /usr/local/lib -lxml2

如果是c檔案,則應該改為:gcc xmlCreator.cpp -o xmlCreator -I /usr/local/include/libxml2  -L /usr/local/lib -lxml2

******-I 後接標頭檔目錄 -L後接lib庫目錄

 

......$ 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

 

 因此,出現這種錯誤的時候,要注意看是不是編譯器用錯了,用gcc編譯C程式,用g++編譯C++程式,對號入座就沒有問題了。

產生xmlCreator檔案後,運行結果如下:

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>

到此,一個使用libxml2庫建立xml檔案的簡單例子就講完了。。。。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.