如何使用的libxml2来修改现有的xml文件? [英] how to use libxml2 to modify an existing xml file?

查看:848
本文介绍了如何使用的libxml2来修改现有的xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要把现有的XML文件,并修改只是几个属性和写文件的退了出去。

我在考虑使用libxml2的得到这个工作的。应用程序是C / C ++在Linux上运行。

事情是,libxml2的似乎包括厨房水槽的一些变化,便携式厕所,淋浴间,并通过相同的管道连接的其他各种顺水推舟。有可用不同的解析器,以及做事的方式不同。人谁没有使用过libxml2的,这是一个有点吓人。

例如什么我应该看着,这样在年底,我的输出的.xml等同于原始输入文件,再加上改变我做了?到目前为止,我一直用的libxml2的tree1.c,tree2.c和reader1.c例子玩,但只有这些输出XML不会是接近相同的任何地方。


解决方案

 的#include<的libxml / xmlmemory.h>
#包括LT&;的libxml / parser.h>
#包括LT&;的libxml / xpath.h>//加载磁盘xml文件
xmlDocPtr PDOC = xmlParseFile(file.xml);
//或者从字符串xmlDocPtr PDOC = xmlNewDoc(<根和GT;<元/ GT&;< /根>);//做一些与文档
// ....//保存文档回到磁盘。
xmlSaveFileEnc(file.xml,PDOC,UTF-8);

您想最主要的有可能是这些功能:

 的xmlNodePtr pNode = xmlNewNode(0,(XMLCHAR *)newNodeName);
xmlNodeSetContent(pNode,(XMLCHAR *)内容);
xmlAddChild(pParentNode,pNode);
xmlDocSetRootElement(PDOC,pParentNode);

这里是使用XPath选择的东西一个简单的例子:

  //选取所有的用户节点
XMLCHAR * PEX pression((* XMLCHAR)_ T(/用户/用户));
xmlXPathObjectPtr presultingXPathObject(getnodeset(PDOC,PEX pression));
如果(presultingXPathObject)
{
xmlNodeSetPtr pNodeSet(presultingXPathObject-> nodesetval);
的for(int i = 0; I< pNodeSet-> nodeNr ++ I)
{
的xmlNodePtr pUserNode(pNodeSet-> nodeTab [I]);
                   //做一些事情与节点
}
}
xmlXPathFreeObject(presultingXPathObject);

I need to take an existing xml file, and modify just a few attributes and write the file back out.

I was thinking of using libxml2 to get this done. Application is C/C++ running on Linux.

Thing is, libxml2 seems to include several variations of the kitchen sink, along with portable washrooms, showers, and various other things connected via the same plumbing. There are different parsers available, and different ways of doing things. For someone who hasn't used libxml2 before, this is a bit intimidating.

What example should I be looking at, so that in the end, my output .xml is identical to the original input file, plus the changes I've made? So far, I've been playing with libxml2's tree1.c, tree2.c, and reader1.c examples, but with just these the output xml wont be anywhere near the same.

解决方案

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>

//Load in the xml file from disk
xmlDocPtr pDoc = xmlParseFile("file.xml");
//Or from a string xmlDocPtr pDoc = xmlNewDoc("<root><element/></root>");

//Do something with the document
//....

//Save the document back out to disk.
xmlSaveFileEnc("file.xml", pDoc, "UTF-8");

The main things you want are probably these functions:

xmlNodePtr pNode = xmlNewNode(0, (xmlChar*)"newNodeName");
xmlNodeSetContent(pNode, (xmlChar*)"content");
xmlAddChild(pParentNode, pNode);
xmlDocSetRootElement(pDoc, pParentNode);

And here is a quick example of using xpath to select things:

//Select all the user nodes
xmlChar *pExpression((xmlChar*)_T("/users/user"));
xmlXPathObjectPtr pResultingXPathObject(getnodeset(pDoc, pExpression));
if (pResultingXPathObject)
{
	xmlNodeSetPtr pNodeSet(pResultingXPathObject->nodesetval);
	for(int i = 0; i < pNodeSet->nodeNr; ++i) 
	{
		xmlNodePtr pUserNode(pNodeSet->nodeTab[i]);
                   //do something with the node
	}
}
xmlXPathFreeObject(pResultingXPathObject);

这篇关于如何使用的libxml2来修改现有的xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆