如何在 cpp 中使用 libxml2 创建 DOM
How to create a DOM using libxml2 in cpp
我想为xml创建一个DOM。最后,DOM 应该分配给以下指针。
xmlDocPtr pDOM;
我想要 cpp 示例。帮帮我。
示例 XML 如下所示。
<root>
<a>
<b attr="I want to get this"> </b>
</a>
</root>
LibXML2 的网站上有代码示例:
Libxml2 示例集
具体而言,请看以下示例:
tree1.c:导航树以打印元素名称
/**
* section: Tree
* synopsis: Navigates a tree to print element names
* purpose: Parse a file to a tree, use xmlDocGetRootElement() to
* get the root element, then walk the document and print
* all the element name in document order.
* usage: tree1 filename_or_URL
* test: tree1 test2.xml > tree1.tmp && diff tree1.tmp $(srcdir)/tree1.res
* author: Dodji Seketeli
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#ifdef LIBXML_TREE_ENABLED
/*
*To compile this file using gcc you can type
*gcc `xml2-config --cflags --libs` -o xmlexample libxml2-example.c
*/
/**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* Prints the names of the all the xml elements
* that are siblings or children of a given xml node.
*/
static void
print_element_names(xmlNode * a_node)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("node type: Element, name: %sn", cur_node->name);
}
print_element_names(cur_node->children);
}
}
/**
* Simple example to parse a file called "file.xml",
* walk down the DOM, and print the name of the
* xml elements nodes.
*/
int
main(int argc, char **argv)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
if (argc != 2)
return(1);
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
/*parse the file and get the DOM */
doc = xmlReadFile(argv[1], NULL, 0);
if (doc == NULL) {
printf("error: could not parse file %sn", argv[1]);
}
/*Get the root element node */
root_element = xmlDocGetRootElement(doc);
print_element_names(root_element);
/*free the document */
xmlFreeDoc(doc);
/*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser();
return 0;
}
#else
int main(void) {
fprintf(stderr, "Tree support not compiled inn");
exit(1);
}
#endif
tree2.c:创建树
/*
* section: Tree
* synopsis: Creates a tree
* purpose: Shows how to create document, nodes and dump it to stdout or file.
* usage: tree2 <filename> -Default output: stdout
* test: tree2 > tree2.tmp && diff tree2.tmp $(srcdir)/tree2.res
* author: Lucas Brasilino <brasilino@recife.pe.gov.br>
* copy: see Copyright for the status of this software
*/
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
/*
*To compile this file using gcc you can type
*gcc `xml2-config --cflags --libs` -o tree2 tree2.c
*/
/* A simple example how to create DOM. Libxml2 automagically
* allocates the necessary amount of memory to it.
*/
int
main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
xmlDtdPtr dtd = NULL; /* DTD pointer */
char buff[256];
int i, j;
LIBXML_TEST_VERSION;
/*
* Creates a new document, a node and set it as a root node
*/
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "root");
xmlDocSetRootElement(doc, root_node);
/*
* Creates a DTD declaration. Isn't mandatory.
*/
dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
/*
* xmlNewChild() creates a new node, which is "attached" as child node
* of root_node node.
*/
xmlNewChild(root_node, NULL, BAD_CAST "node1",
BAD_CAST "content of node 1");
/*
* The same as above, but the new child node doesn't have a content
*/
xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);
/*
* xmlNewProp() creates attributes, which is "attached" to an node.
* It returns xmlAttrPtr, which isn't used here.
*/
node =
xmlNewChild(root_node, NULL, BAD_CAST "node3",
BAD_CAST "this node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");
/*
* Here goes another way to create nodes. xmlNewNode() and xmlNewText
* creates a node and a text node separately. They are "attached"
* by xmlAddChild()
*/
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST
"other way to create content (which is also a node)");
xmlAddChild(node, node1);
xmlAddChild(root_node, node);
/*
* A simple loop that "automates" nodes creation
*/
for (i = 5; i < 7; i++) {
sprintf(buff, "node%d", i);
node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
for (j = 1; j < 4; j++) {
sprintf(buff, "node%d%d", i, j);
node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
xmlNewProp(node1, BAD_CAST "odd", BAD_CAST((j % 2) ? "no" : "yes"));
}
}
/*
* Dumping document to stdio or file
*/
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
/*free the document */
xmlFreeDoc(doc);
/*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
return(0);
}
#else
int main(void) {
fprintf(stderr, "tree support not compiled inn");
exit(1);
}
#endif
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 如何在 cpp 中使用 libxml2 创建 DOM