libxml2常用函数

来源:互联网 发布:美化二维码制作软件 编辑:程序博客网 时间:2024/05/08 04:27

1.创建文件时常用的函数及其作用
创建xml文档中需要:新建文件、新建节点、设置节点内容、设置节点属性、建立节点之间的关系、保存文档等;
(1)xmlNewDoc()
原型:xmlDocPtr xmlNewDoc (const xmlChar *version);
参数:version : xmlChar string giving the version of XML “1.0”
返回值:Returns : a new document
功能:生成一个xml文档

(2)xmlNewNode()
函数原型:xmlNodePtr xmlNewNode (xmlNsPtr ns, const xmlChar *name);
参数: ns : namespace if any
name : the node name
返回值:Returns : a pointer to the new node object.
功能:Creation of a new node element. ns is optional (NULL).

(3)xmlNodeSetContent ()
函数原型:void xmlNodeSetContent (xmlNodePtr cur, const xmlChar *content);
参数:cur : the node being modified
content : the new value of the content
功能:Replace the content of a node.

(4)xmlNewProp ()
函数原型:xmlAttrPtr xmlNewProp (xmlNodePtr node, const xmlChar *name, const xmlChar *value);
参数:node : the holding node
name : the name of the attribute
value : the value of the attribute
返回值:Returns : a pointer to the attribute
功能:Create a new property carried by a node.

(5)xmlAddChild ()
xmlNodePtr xmlAddChild (xmlNodePtr parent, xmlNodePtr cur);
参数:parent : the parent node
cur : the child node
返回值:Returns : the child or NULL in case of error.
功能:将一个节点设置为另外一个节点的子节点

(6)xmlDocSetRootElement ()
函数原型:xmlNodePtr xmlDocSetRootElement (xmlDocPtr doc, xmlNodePtr root);
参数:doc : the document
root : the new document root element
Returns : the old root element if any was found
功能:Set the root element of the document (doc->children is a list containing possibly comments, PIs, etc …).
设置某个节点为整个文档的根节点

(7)xmlDocDumpMemory ()
函数原型:void xmlDocDumpMemory (xmlDocPtr cur,xmlChar **mem,int *size);
参数:cur : the document
mem : OUT: the memory pointer
size : OUT: the memory length
功能:Dump an XML document in memory and return the xmlChar * and it’s size. It’s up to the caller to free the memory with xmlFree().
将生成的xml DOM树保存到内存中(不是硬盘)

(8)xmlDocDumpFormatMemory ()
void xmlDocDumpFormatMemory (xmlDocPtr cur,xmlChar **mem, int *size,int format);
与xmlDocDumpMemory ()类似,区别在于可以选择保存的格式
此系列函数还包括:
xmlDocDump、xmlDocDumpFormatMemoryEnc、 xmlDocDumpMemory、xmlDocDumpMemoryEnc、xmlDocFormatDump

(9)xmlSaveFormatFileEnc ()
函数原型:int xmlSaveFormatFileEnc (const char *filename,xmlDocPtr cur,const char *encoding,int format);
参数:filename : the filename or URL to output
cur : the document being saved
encoding : the name of the encoding to use or NULL.
format : should formatting spaces be added.
Returns : the number of bytes written or -1 in case of error.
功能:将xml文件保存在硬盘
此系列的函数还包括:xmlSaveFileEnc
特别注意一种情况:将xml形式的字符串转换为xml文档的函数xmlParseMemory()和xmlParseDoc();

2.解析xml文档
解析xml文档的过程中可能有的操作:将文件导入、获得节点的属性、获得节点的内容、
(1)获得节点内容函数xmlNodeGetContent ()
函数原型:xmlChar* xmlNodeGetContent (xmlNodePtr cur);
参数: cur : the node being read
返回值:Returns : a new xmlChar * or NULL if no content is available. It’s up to the caller to free the memory with xmlFree().
功能:Read the value of a node, this can be either the text carried directly by this node if it’s a TEXT node or the aggregate string of the values carried by this node child’s (TEXT and ENTITY_REF). Entity references are substituted.

(2)获得节点属性函数xmlGetProp ()
函数原型:xmlChar* xmlGetProp (xmlNodePtr node,const xmlChar *name);
参数:node : the node
name : the attribute name
返回值:Returns : the attribute value or NULL if not found. It’s up to the caller to free the memory with xmlFree().
功能:Search and get the value of an attribute associated to a node This does the entity substitution. This function looks in DTD attribute declaration for FIXED or default declaration values unless DTD use has been turned off.

(3)读入xml字符形式的文件xmlReadfile()
使用xmlSaveFormatFileEnc()将xml文档存为文件之后,可以使用xmlReadfile读入
xmlDocPtr doc=xmlReadFile(szDocname,”UTF-8”,XML_PARSE_RECOVER);
doc = xmlReadFile(szDocName, “GB2312”, XML_PARSE_RECOVER);//以GB2312的编码方式读入解析文件

(4)获得根节点
xmlNodePtr xmlDocGetRootElement(xmlDocPtr doc)

(5)设置xml文档的上下文指针
context = xmlXPathNewContext(pdoc);

(6)计算满足xpath表达式的节点
result = xmlXPathEvalExpression(xpath, context);

相关链接:http://www.open-open.com/lib/view/open1422537148658.html
http://blog.csdn.net/iamlate/article/details/6955698
http://blog.csdn.net/hanchaoman/article/details/42557195
http://blog.chinaunix.net/uid-114518-id-2873824.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我在综漫世界推销精灵 我不可能会录这种自述 没人比我更懂禁忌 四合院之飞扬年代 50块上单,只好去LCK打替补 莽荒纪之长生之路 死神:从统治虚圈开始 这位公子深藏不露 神话之门 诸天抽奖:开局段誉巅峰内力 杀手的退休柯学纪实 借神长生:从拐走仇人的道侣开始 暮年修仙的我长生不死 无尽海域,我以养妖求长生 吞噬星空之我躺平了也变强了 跑山人:我的属性加点人生 从九牛二虎开始无敌 高达SEED之前进四 龙族:从道诡异仙归来的路明非 穿越凡人我能无限升级 超神:无限知识,我带文明飞升! 重回八二做个好人 从斗罗开始:杀戮之都遇比比东 秦时:从监禁焰灵姬开始 重生之四合院的日子 权游之凛冬龙主 美利坚:我就是华尔街大公牛 我的宠兽词条过于离谱 末世诡海:全民加点孤艇求生 超神:开局奖励修罗铠甲 我在火影苟到无敌 我,巨星,劝退粉丝 身为草神:我无敌,你随意 娱乐:让你录综艺,你成影帝了? 关于我成了祖国人这档事 港综从神诡秘法开始 遮天之问道永恒 长生不死,从洞天福地开始 黑科技农场 谍海风雷 我在诡世修仙