转自: http://www.jb51.cc/article/p-aeshvcmd-op.html
两个函数,怎么在内存中处理xml文件。有个疑问,xmlChar *xmlbuff;这样系统第一次分配了xmlbuff的大小以后。对xml做修改,可以成功吗。
有空试试。
函数原型:
- xmlParseMemory
- xmlDocPtrxmlParseMemory(constchar*buffer,
- intsize)
- parseanXMLin-memoryblockandbuildatree.
- buffer:anpointertoachararray
- size:thesizeofthearray
- Returns:theresultingdocumenttree
- Function:xmlParserAddNodeIn
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@H_502_84@代码示例:
- 解析xml字符串
- xmlDocPtrdoc=xmlParseMemory(pXml,length);
- //根据xmldoc获得xml的根节点
- xmlNodePtrcur=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);
- //根节点相关函数
- xmlNodePtrxmlDocGetRootElement(xmlDocPtrdoc)//获取文档根节点
- xmlNodePtrxmlDocSetRootElement(xmlDocPtrdoc,xmlNodePtrroot)//设置文档根节点
- //创建子节点相关函数
- xmlNodePtrxmlNewNode(xmlNsPtrns,constxmlChar*name)//创建新节点
- xmlNodePtrxmlNewChild(xmlNodePtrparent,xmlNsPtrns,constxmlChar*name,constxmlChar*content)//创建新的子节点
- xmlNodePtrxmlCopyNode(constxmlNodePtrnode,intextended)//复制当前节点
- //添加子节点相关函数
- xmlNodePtrxmlAddChild(xmlNodePtrparent,xmlNodePtrcur)//给指定节点添加子节点
- xmlNodePtrxmlAddNextSibling(xmlNodePtrcur,xmlNodePtrelem)//添加后一个兄弟节点
- xmlNodePtrxmlAddPrevSibling(xmlNodePtrcur,xmlNodePtrelem)//添加前一个兄弟节点
- xmlNodePtrxmlAddSibling(xmlNodePtrcur,xmlNodePtrelem)//添加兄弟节点
- //属性相关函数
- xmlAttrPtrxmlNewProp(xmlNodePtrnode,constxmlChar*value)//创建新节点属性
- xmlChar*xmlGetProp(xmlNodePtrnode,constxmlChar*name)//读取节点属性
- xmlAttrPtrxmlSetProp(xmlNodePtrnode,constxmlChar*value)//设置节点属性
- =xmlNodeListGetstring(doc,1);
- =xmlNodeContent(cur);
解析xml字符串 xmlDocPtr doc = xmlParseMemory(pXml,length); //根据xmldoc获得xml的根节点 xmlNodePtr cur = xmlDocGetRootElement(doc); //获得子节点:->children获得不是第一个子节点,必须用next才能获得第一个子节点 cur = cur->children; cur = cur->next; // 获得节点信息中的内容: 注意释放资源 xmlChar* key = xmlNodeListGetString(doc,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 * value) //创建新节点属性 xmlChar * xmlGetProp (xmlNodePtr node,const xmlChar * name) //读取节点属性 xmlAttrPtr xmlSetProp (xmlNodePtr node,const xmlChar * value) //设置节点属性 =xmlNodeListGetstring(doc,1); =xmlNodeContent(cur); @H_502_84@
===========================================================================================
函数原型:
- Function:xmlDocDumpFormatMemory
- voidxmlDocDumpFormatMemory(xmlDocPtrcur,
- xmlChar**mem,
- int*size,
- intformat)
- DumpanXMLdocumentinmemoryandreturnthe#xmlChar*andit'ssize.It'suptothecallertofreethememorywithxmlFree().Notethat@format=1providenodeindentingonlyifxmlIndentTreeOutput=1orxmlKeepBlanksDefault(0)wascalled
- cur:thedocument
- mem:OUT:thememorypointer
- size:OUT:thememorylength
- format:shouldformattingspacesbeenadded
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@H_502_84@
程序示例:原文链接:https://www.f2er.com/xml/298627.html
- /**
- *section:InputOutput
- *synopsis:Outputtocharbuffer
- *purpose:DemonstratetheuSEOfxmlDocDumpMemory
- *tooutputdocumenttoacharacterbuffer
- *usage:io2
- *test:io2>io2.tmp;diffio2.tmpio2.res;rm-fio2.tmp
- *author:JohnFleck
- *copy:seeCopyrightforthestatusofthissoftware.
- */
- #include<libxml/parser.h>
- #ifdefined(LIBXML_TREE_ENABLED)&&defined(LIBXML_OUTPUT_ENABLED)
- int
- main(void)
- {
- xmlNodePtrn;
- xmlDocPtrdoc;
- xmlChar*xmlbuff;
- intbuffersize;
- /*
- *Createthedocument.
- */
- doc=xmlNewDoc(BAD_CAST"1.0");
- n=xmlNewNode(NULL,BAD_CAST"root");
- xmlNodeSetContent(n,BAD_CAST"content");
- xmlDocSetRootElement(doc,n);
- /*
- *Dumpthedocumenttoabufferandprintit
- *fordemonstrationpurposes.
- */
- xmlDocDumpFormatMemory(doc,&xmlbuff,&buffersize,1);
- printf("%s",(char*)xmlbuff);
- /*
- *Freeassociatedmemory.
- */
- xmlFree(xmlbuff);
- xmlFreeDoc(doc);
- return(0);
- }
- #else
- #include<stdio.h>
- int
- main(void)
- {
- fprintf(stderr,
- "librarynotconfiguredwithtreeandoutputsupport\n");
- return(1);
- }
- #endif