C++ 如何在cpp中使用libxml2创建DOM
我想为xml创建一个DOM。最后,应该将DOM分配给以下指针C++ 如何在cpp中使用libxml2创建DOM,c++,libxml2,C++,Libxml2,我想为xml创建一个DOM。最后,应该将DOM分配给以下指针 xmlDocPtr pDOM; 我想要cpp的例子。 帮帮我 示例xml如下所示 <root> <a> <b attr="I want to get this"> </b> </a> </root> LibXML2的网站上有一些代码示例: 特别是,请看以下示例: /** *章节:树 *简介:导航树以打印元素名称 *用途:将文件解析为树,使用
xmlDocPtr pDOM;
我想要cpp的例子。
帮帮我
示例xml如下所示
<root>
<a>
<b attr="I want to get this"> </b>
</a>
</root>
LibXML2的网站上有一些代码示例: 特别是,请看以下示例:
/**
*章节:树
*简介:导航树以打印元素名称
*用途:将文件解析为树,使用xmlDocGetRootElement()来
*获取根元素,然后遍历文档并打印
*按文档顺序显示所有元素名称。
*用法:树1文件名\u或\u URL
*测试:tree1test2.xml>tree1.tmp&&diff tree1.tmp$(srcdir)/tree1.res
*作者:Dodji Seketeli
*复制:有关此软件的状态,请参阅版权。
*/
#包括
#包括
#包括
#ifdef LIBXML_树已启用
/*
*要使用gcc编译此文件,可以键入
*gcc`xml2 config--cflags--libs`-o xmlexample libxml2 example.c
*/
/**
*打印元素名称:
*@ AyNoto:要考虑的初始XML节点。
*
*打印所有xml元素的名称
*是给定xml节点的同级或子级。
*/
静态空隙
打印元素名称(xmlNode*a节点)
{
xmlNode*cur_node=NULL;
对于(cur_节点=a_节点;cur_节点;cur_节点=cur_节点->下一步){
if(cur\u节点->类型==XML\u元素\u节点){
printf(“节点类型:元素,名称:%s\n”,当前节点->名称);
}
打印元素名称(当前节点->子节点);
}
}
/**
*解析名为“file.xml”的文件的简单示例,
*沿着DOM向下走,并打印
*xml元素节点。
*/
int
主(内部argc,字符**argv)
{
xmlDoc*doc=NULL;
xmlNode*根元素=NULL;
如果(argc!=2)
申报表(1);
/*
*这将初始化库并检查潜在的ABI不匹配
*在编译版本和实际共享版本之间
*图书馆使用。
*/
LIBXML_测试_版本
/*解析文件并获取DOM*/
doc=xmlReadFile(argv[1],NULL,0);
如果(doc==NULL){
printf(“错误:无法分析文件%s\n”,argv[1]);
}
/*获取根元素节点*/
根元素=xmlDocGetRootElement(doc);
打印元素名称(根元素);
/*释放文档*/
xmlFreeDoc(doc);
/*
*释放可能存在的全局变量
*已由解析器分配。
*/
xmlcleanuparser();
返回0;
}
#否则
内部主(空){
fprintf(stderr,“树支持未在\n中编译”);
出口(1);
}
#恩迪夫
/*
*章节:树
*简介:创建一棵树
*用途:演示如何创建文档、节点并将其转储到标准输出或文件。
*用法:tree2-默认输出:stdout
*测试:tree2>tree2.tmp&&diff tree2.tmp$(srcdir)/tree2.res
*作者:卢卡斯·布拉西利诺
*副本:有关此软件的状态,请参阅版权
*/
#包括
#包括
#包括
#如果已定义(已启用LIBXML树)&已定义(已启用LIBXML输出)
/*
*要使用gcc编译此文件,可以键入
*gcc`xml2 config--cflags--libs`-o tree2 tree2.c
*/
/*一个如何创建DOM的简单示例。Libxml2自动
*为其分配必要的内存量。
*/
int
主(内部argc,字符**argv)
{
xmlDocPtr doc=NULL;/*文档指针*/
xmlNodePtr root_node=NULL,node=NULL,node1=NULL;/*节点指针*/
xmlDtdPtr dtd=NULL;/*dtd指针*/
字符buff[256];
int i,j;
LIBXML_测试_版本;
/*
*创建新文档、节点并将其设置为根节点
*/
doc=xmlNewDoc(BAD_CAST“1.0”);
root_node=xmlNewNode(NULL,错误的_转换为“root”);
xmlDocSetRootElement(doc,根节点);
/*
*创建DTD声明。不是必需的。
*/
dtd=xmlCreateIntSubset(doc,BAD_CAST“root”,NULL,BAD_CAST“tree2.dtd”);
/*
*xmlNewChild()创建一个新节点,该节点作为子节点“附加”
*根节点的。
*/
xmlNewChild(根节点,NULL,错误的\u转换“node1”,
错误的“节点1的内容”);
/*
*同上,但新的子节点没有内容
*/
xmlNewChild(根节点,NULL,坏节点类型“node2”,NULL);
/*
*xmlNewProp()创建属性,该属性“附加”到节点。
*它返回xmlatrptr,这里不使用它。
*/
节点=
xmlNewChild(根节点,NULL,错误的\u转换“node3”,
错误的_CAST“此节点具有属性”);
xmlNewProp(节点,BAD_CAST“属性”,BAD_CAST“是”);
xmlNewProp(节点,BAD_CAST“foo”,BAD_CAST“bar”);
/*
*下面是创建nodes.xmlNewNode()和xmlNewText的另一种方法
*分别创建节点和文本节点。它们是“附加的”
*由xmlAddChild()编写
*/
node=xmlNewNode(NULL,错误类型为“node4”);
node1=xmlNewText(错误类型)
“创建内容的其他方式(也是节点)”;
xmlAddChild(节点,node1);
xmlAddChild(根节点,节点);
/*
*“自动化”节点创建的简单循环
*/
对于(i=5;i<7;i++){
sprintf(buff,“节点%d”,i);
node=xmlNewChild(根节点,NULL,坏的抛出buff,NULL);
对于(j=1;j<4;j++){
sprintf(buff,“节点%d%d”,i,j);
node1=xmlNewChild(node,NULL,BAD_CAST buff,NULL);
xmlNewProp(节点1,坏演员“奇数”,坏演员((j%2)?“否”:“是”);
}
}
/*
*将文档转储到stdio或文件
*/
xmlSaveFormatFileEnc(argc>1?argv[1]:“-”,doc,“UTF-8”,1);
/*释放文档*/
xmlFreeDoc(doc);
/*
*释放可能存在的全局变量
*已由解析器分配。
*/
xmlcleanuparser();
/*
*这是为了调试回归测试的内存
*/
xmlMemoryDump();
返回(0);
}
#否则
内部主(空){
fprintf(stderr,“树支持未在\n中编译”);
出口(1);
}
#恩迪夫
LibXML2的w上有一些代码示例
/**
* 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: %s\n", 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 %s\n", 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 in\n");
exit(1);
}
#endif
/*
* 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 in\n");
exit(1);
}
#endif