如何使用libxml2来修改现有的xml文件?

时间:2022-08-29 07:14:05

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

我需要使用一个现有的xml文件,只修改几个属性并将文件写回。

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

我想使用libxml2来完成这一任务。应用程序是在Linux上运行的C/ c++。

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.

事实是,libxml2似乎包含了厨房水槽的几个变体,以及通过相同的管道连接的便携式盥洗室、淋浴器和各种其他东西。有不同的解析器可用,也有不同的处理方法。对于以前没有使用过libxml2的人来说,这有点吓人。

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.

最后,我的输出。xml与原始输入文件以及我所做的更改完全相同,我应该查看哪个示例?到目前为止,我一直在使用libxml2的tree1。c,tree2。c,reader1。c示例,但是只有这些输出xml不会出现在相同的地方。

1 个解决方案

#1


21  

#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:

下面是一个使用xpath进行选择的简单示例:

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

#1


21  

#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:

下面是一个使用xpath进行选择的简单示例:

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