libxml2 c库使用

2023-11-09

##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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

libxml2 c库使用 的相关文章

随机推荐