兩個函數,怎麼在記憶體中處理xml檔案。有個疑問,xmlChar *xmlbuff;這樣系統第一次配置設定了xmlbuff的大小以後。對xml做修改,可以成功嗎。
有空試試。
函數原型:
xmlParseMemory
xmlDocPtr xmlParseMemory (const char * buffer,
int size)
parse an XML in-memory block and build a tree.
buffer: an pointer to a char array
size: the size of the array
Returns: the resulting document tree
Function: xmlParserAddNodeIn
代碼示例:
解析xml字元串
xmlDocPtr doc = xmlParseMemory(pXml, length);
//根據xmldoc獲得xml的根節點
xmlNodePtr cur = xmlDocGetRootElement(doc);
//獲得子節點:->children獲得不是第一個子節點,必須用next才能獲得第一個子節點
cur = cur->children;
cur = cur->next;
// 獲得節點資訊中的内容: 注意釋放資源
xmlChar* key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
xmlFree(key);
//獲得節點資訊屬性的值:屬性name,注意釋放資源
xmlChar* fversion = xmlGetProp(cur, "version");
xmlFree(fversion);
//根節點相關函數
xmlNodePtr xmlDocGetRootElement (xmlDocPtr doc) //擷取文檔根節點
xmlNodePtr xmlDocSetRootElement (xmlDocPtr doc, xmlNodePtr root) //設定文檔根節點
//建立子節點相關函數
xmlNodePtr xmlNewNode (xmlNsPtr ns, const xmlChar * name) //建立新節點
xmlNodePtr xmlNewChild (xmlNodePtr parent, xmlNsPtr ns, const xmlChar * name, const xmlChar * content) //建立新的子節點
xmlNodePtr xmlCopyNode (const xmlNodePtr node, int extended) //複制目前節點
//添加子節點相關函數
xmlNodePtr xmlAddChild (xmlNodePtr parent, xmlNodePtr cur) //給指定節點添加子節點
xmlNodePtr xmlAddNextSibling (xmlNodePtr cur, xmlNodePtr elem) //添加後一個兄弟節點
xmlNodePtr xmlAddPrevSibling (xmlNodePtr cur, xmlNodePtr elem) //添加前一個兄弟節點
xmlNodePtr xmlAddSibling (xmlNodePtr cur, xmlNodePtr elem) //添加兄弟節點
//屬性相關函數
xmlAttrPtr xmlNewProp (xmlNodePtr node, const xmlChar * name, const xmlChar * value) //建立新節點屬性
xmlChar * xmlGetProp (xmlNodePtr node, const xmlChar * name) //讀取節點屬性
xmlAttrPtr xmlSetProp (xmlNodePtr node, const xmlChar * name, const xmlChar * value) //設定節點屬性
=xmlNodeListGetstring(doc, cur->xmlChildrenNode, 1);
=xmlNodeContent(cur);
===========================================================================================
函數原型:
Function: xmlDocDumpFormatMemory
void xmlDocDumpFormatMemory (xmlDocPtr cur,
xmlChar ** mem,
int * size,
int format)
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
cur: the document
mem: OUT: the memory pointer
size: OUT: the memory length
format: should formatting spaces been added
程式示例:
/**
* section: InputOutput
* synopsis: Output to char buffer
* purpose: Demonstrate the use of xmlDocDumpMemory
* to output document to a character buffer
* usage: io2
* test: io2 > io2.tmp ; diff io2.tmp io2.res ; rm -f io2.tmp
* author: John Fleck
* copy: see Copyright for the status of this software.
*/
#include <libxml/parser.h>
#if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
int
main(void)
{
xmlNodePtr n;
xmlDocPtr doc;
xmlChar *xmlbuff;
int buffersize;
/*
* Create the document.
*/
doc = xmlNewDoc(BAD_CAST "1.0");
n = xmlNewNode(NULL, BAD_CAST "root");
xmlNodeSetContent(n, BAD_CAST "content");
xmlDocSetRootElement(doc, n);
/*
* Dump the document to a buffer and print it
* for demonstration purposes.
*/
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
printf("%s", (char *) xmlbuff);
/*
* Free associated memory.
*/
xmlFree(xmlbuff);
xmlFreeDoc(doc);
return (0);
}
#else
#include <stdio.h>
int
main(void)
{
fprintf(stderr,
"library not configured with tree and output support\n");
return (1);
}
#endif
來源:
http://xmlsoft.org/examples/io2.c //libxml官方網站
http://blog.163.com/miky_sun/blog/static/3369405201041942853395/