libxml库之xmlParseMemory和xmlDocDumpFormatMemory函数 .

来源:互联网 发布:淘宝css代码生成器 编辑:程序博客网 时间:2024/06/10 17:14

转自: http://blog.csdn.net/iamlate/article/details/6955698

 

两个函数,怎么在内存中处理xml文件。有个疑问,xmlChar *xmlbuff;这样系统第一次分配了xmlbuff的大小以后。对xml做修改,可以成功吗。

有空试试。


函数原型:

[cpp] view plaincopyprint?
  1. xmlParseMemory  
  2.   
  3. xmlDocPtr   xmlParseMemory      (const char * buffer,   
  4.                      int size)  
  5.   
  6. parse an XML in-memory block and build a tree.  
  7.   
  8. buffer: an pointer to a char array  
  9. size:   the size of the array  
  10. Returns:    the resulting document tree  
  11. Function: xmlParserAddNodeIn  


代码示例:
[cpp] view plaincopyprint?
  1.  解析xml字符串  
  2. xmlDocPtr doc = xmlParseMemory(pXml, length);  
  3.    
  4. //根据xmldoc获得xml的根节点   
  5. xmlNodePtr cur = xmlDocGetRootElement(doc);  
  6.    
  7. //获得子节点:->children获得不是第一个子节点,必须用next才能获得第一个子节点  
  8. cur = cur->children;  
  9. cur = cur->next;  
  10.   
  11. // 获得节点信息中的内容: 注意释放资源   
  12. xmlChar* key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);  
  13. xmlFree(key);  
  14.   
  15. //获得节点信息属性的值:属性name,注意释放资源   
  16. xmlChar* fversion = xmlGetProp(cur, "version");  
  17. xmlFree(fversion);  
  18.   
  19.   
  20.   
  21. //根节点相关函数   
  22. xmlNodePtr xmlDocGetRootElement (xmlDocPtr doc) //获取文档根节点  
  23. xmlNodePtr xmlDocSetRootElement (xmlDocPtr doc, xmlNodePtr root) //设置文档根节点  
  24.   
  25. //创建子节点相关函数   
  26. xmlNodePtr xmlNewNode (xmlNsPtr ns, const xmlChar * name) //创建新节点  
  27. xmlNodePtr xmlNewChild (xmlNodePtr parent, xmlNsPtr ns, const xmlChar * name, const xmlChar * content) //创建新的子节点  
  28. xmlNodePtr xmlCopyNode (const xmlNodePtr node, int extended) //复制当前节点  
  29.   
  30. //添加子节点相关函数   
  31. xmlNodePtr xmlAddChild (xmlNodePtr parent, xmlNodePtr cur) //给指定节点添加子节点  
  32. xmlNodePtr xmlAddNextSibling (xmlNodePtr cur, xmlNodePtr elem) //添加后一个兄弟节点  
  33. xmlNodePtr xmlAddPrevSibling (xmlNodePtr cur, xmlNodePtr elem) //添加前一个兄弟节点  
  34. xmlNodePtr xmlAddSibling (xmlNodePtr cur, xmlNodePtr elem) //添加兄弟节点  
  35.   
  36. //属性相关函数   
  37. xmlAttrPtr    xmlNewProp (xmlNodePtr node, const xmlChar * name, const xmlChar * value) //创建新节点属性  
  38. xmlChar *    xmlGetProp (xmlNodePtr node, const xmlChar * name) //读取节点属性  
  39. xmlAttrPtr    xmlSetProp (xmlNodePtr node, const xmlChar * name, const xmlChar * value) //设置节点属性  
  40.   
  41. =xmlNodeListGetstring(doc, cur->xmlChildrenNode, 1);  
  42. =xmlNodeContent(cur);  





===========================================================================================

函数原型:

[cpp] view plaincopyprint?
  1. Function: xmlDocDumpFormatMemory  
  2.   
  3. void    xmlDocDumpFormatMemory      (xmlDocPtr cur,   
  4.                      xmlChar ** mem,   
  5.                      int * size,   
  6.                      int format)  
  7.   
  8. 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(). Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called  
  9.   
  10. cur:    the document  
  11. mem:    OUT: the memory pointer  
  12. size:   OUT: the memory length  
  13. format: should formatting spaces been added  

程序示例:

[cpp] view plaincopyprint?
  1. /** 
  2.  * section: InputOutput 
  3.  * synopsis: Output to char buffer 
  4.  * purpose: Demonstrate the use of xmlDocDumpMemory 
  5.  *          to output document to a character buffer 
  6.  * usage: io2 
  7.  * test: io2 > io2.tmp ; diff io2.tmp io2.res ; rm -f io2.tmp 
  8.  * author: John Fleck 
  9.  * copy: see Copyright for the status of this software. 
  10.  */  
  11.   
  12. #include <libxml/parser.h>   
  13.   
  14. #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)  
  15. int  
  16. main(void)  
  17. {  
  18.   
  19.     xmlNodePtr n;  
  20.     xmlDocPtr doc;  
  21.     xmlChar *xmlbuff;  
  22.     int buffersize;  
  23.   
  24.     /* 
  25.      * Create the document. 
  26.      */  
  27.     doc = xmlNewDoc(BAD_CAST "1.0");  
  28.     n = xmlNewNode(NULL, BAD_CAST "root");  
  29.     xmlNodeSetContent(n, BAD_CAST "content");  
  30.     xmlDocSetRootElement(doc, n);  
  31.   
  32.     /* 
  33.      * Dump the document to a buffer and print it 
  34.      * for demonstration purposes. 
  35.      */  
  36.     xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);  
  37.     printf("%s", (char *) xmlbuff);  
  38.   
  39.     /* 
  40.      * Free associated memory. 
  41.      */  
  42.     xmlFree(xmlbuff);  
  43.     xmlFreeDoc(doc);  
  44.   
  45.     return (0);  
  46.   
  47. }  
  48. #else   
  49. #include <stdio.h>   
  50.   
  51. int  
  52. main(void)  
  53. {  
  54.     fprintf(stderr,  
  55.             "library not configured with tree and output support\n");  
  56.     return (1);  
  57. }  
  58. #endif  
0 0
原创粉丝点击