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);