libxml2 c庫使用

##libxml2庫

1、讀取一個文件到內存

xmlParseFile和xmlReadFile:xmlReadFile() is a bit more powerful as it is able to take an URL instead of a local file path,同時還帶參。一般用xmlReadFile

xmlReadFile、xmlParserOption:http://xmlsoft.org/html/libxml-parser.html#xmlParserOption

xmlDoc

 

2、獲取節點

xmlNode(children、prev\last\parent)

xmlDocGetRootElement

 

3、新doc的組建

http://www.xmlsoft.org/examples/tree2.c

 

doc = xmlNewDoc(BAD_CAST "1.0");

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

xmlDocSetRootElement(doc, root_node);

node=xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);

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

 

添加node及屬性的方式:

xmlNewChild(root_node, NULL, BAD_CAST "node1",

BAD_CAST "content of node 1");

/*

* The same as above, but the new child node doesn't have a content

*/

xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);

 

/*

* xmlNewProp() creates attributes, which is "attached" to an node.

* It returns xmlAttrPtr, which isn't used here.

*/

node =

xmlNewChild(root_node, NULL, BAD_CAST "node3",

BAD_CAST "this node has attributes");

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

xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");

 

/*

* Here goes another way to create nodes. xmlNewNode() and xmlNewText

* creates a node and a text node separately. They are "attached"

* by xmlAddChild()

*/

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

node1 = xmlNewText(BAD_CAST

"other way to create content (which is also a node)");

xmlAddChild(node, node1);

xmlAddChild(root_node, node);

 

xmlDoc屬性:

xmlNode屬性:name,節點名稱,xmlNodeListGetString獲取節點的文本內容(文本內容是節點的子節點) https://blog.csdn.net/mcgrady_tracy/article/details/46386707

xmlGetProp獲取節點屬性:

xmlChar* attr_value = NULL;

if(!xmlStrcmp(node->name, (const xmlChar*)"node2")) {

attr_value = xmlGetProp(node, "attribute");

printf("attribute value:%s\n",attr_value);

xmlFree(attr_value);

}

 

4、查找

方法1:遍歷,children,prev,next,parent

方法2:xpath libxml2 supports XPath 1.0 語法定義:https://www.w3.org/TR/1999/REC-xpath-19991116/

xmlXPathContextPtr context;

xmlXPathObjectPtr result; /* 存儲查詢結果 */

 

/* 創建一個xpath上下文 */

context = xmlXPathNewContext(doc);

result = xmlXPathEvalExpression(xpath, context);

xmlXPathFreeContext(context); /* 釋放上下文指針 *

if(xmlXPathNodeSetIsEmpty(result->nodesetval)){

xmlXPathFreeObject(result); /* 如爲這空就釋放 */

printf("No result\n");

return NULL;

}

return result;

 

xpath語法符合通用xpath語法,如果值爲字符串,用單引號括起:定界操作 + 定位操作

 

ICONV是一個專門用來進行編碼轉換的庫,基本上支持目前所有常用的編碼。它是glibc庫的一個部分,常常被用於UNIX系統中。

 

5、刪除節點

if(!xmlStrcmp(cur->name, BAD_CAST "keyword")){

xmlNodePtr tempNode;

tempNode = cur->next;

xmlUnlinkNode(cur);

xmlFreeNode(cur);

cur = tempNode;

continue;

}

注意libxml2並沒有xmlDelNode或者xmlRemoveNode之類的函數。我們需要將當前節點從文檔中斷鏈(unlink),文檔就不會再包含這個子節點。

這樣做需要使用一個臨時變量來存儲斷鏈節點的後續節點,並記得要手動刪除斷鏈節點的內存。

 

6、xsd驗證

xmlSchemaValidateDoc

對於不復雜的數據,在程序中寫入之前進行判斷就ok,無需調用此函數判斷。

 

7、附加xml工具

xmllint是一個很方便的處理及驗證xml的工具,linux下只要安裝libxml2就可以使用這個命令。功能有格式化、驗證xsd

xml編輯器:oxygen xml editor

 

xmllint --noout your_test_file.xml

 

8、編碼問題

Libxml2本身只支持把UTF-8, UTF-16和ISO-8859-1格式的外部數據轉換成內部使用的UTF-8格式,以及處理完後輸出成這些格式的數據。

對其他的字符編碼,需要使用libiconv(當然你也可以使用其他的國際化庫,例如ICU)。當前libiconv支持150多種不同的字符編碼,

libiconv的實現儘量保證支持所有我們聽過的編碼格式。在使用libxml之前,一般是通過libiconv把數據先轉換UTF-8格式。

在使用libxml處理完之後,再通過libiconv把數據輸出成你要的編碼格式。

 

 

9、內存釋放與清理:

/*

*Free the global variables that may

*have been allocated by the parser.

*/

xmlCleanupParser();

 

/*

* this is to debug memory for regression tests

*/

xmlMemoryDump();

 

10、寫入文件

 

xmlSaveFormatFileEnc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章