Author:张继飞
插入节点部分的实现是从现有节点的后面添加。
为了解决插入后不对齐的问题,需要修改这几个地方
doc=xmlParseFile("xml.xml"); 改为 doc=xmlReadFile("xml.xml","UTF-8",XML_PARSE_NOBLANKS);
xmlSaveFormatFile("xml.xml", doc, 0); 改为 xmlSaveFormatFile("xml.xml", doc, 1);
#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main(int argc, char **argv)
{///insert message into xml.xml 该部分为在相关xml文件插入节点
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
doc=xmlParseFile("xml.xml"); //首先解析xml文件,放入内存
root_node = xmlDocGetRootElement(doc); //获取根节点
//creates a new node, which is "attached" as child node of root_node node.
node = xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1"); //根据根节点(root_node)来插入根节点的的子节点为node1,
xmlNewChild(node, NULL, BAD_CAST "node2",BAD_CAST "node2 is node1's sub"); //根据子节点node1(node)来插入子节点的的子节点为node2,
node=xmlNewChild(root_node, NULL, BAD_CAST "node3",BAD_CAST"node has attributes"); //插入根节点的子节点node3
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes"); //添加子节点的属性。
//Here goes another way to create nodes.
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST"other way to create content");
xmlAddChild(node, node1);
xmlAddChild(root_node, node); //Dumping document to stdio or file
xmlSaveFormatFile("xml.xml", doc, 0); //将此内存保存到文件
xmlFreeDoc(doc); /*free the document */
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}
/*
int main(int argc, char **argv)
{///creat xml doc 该部分为创建xml文件
xmlDocPtr doc = NULL; // document pointer
xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;// node pointers
// 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 new node, which is "attached" as child node of root_node node.
xmlNewChild(root_node, NULL, BAD_CAST "node1",BAD_CAST "content of node1");
// xmlNewProp() creates attributes, which is "attached" to an node.
node=xmlNewChild(root_node, NULL, BAD_CAST "node3",BAD_CAST"node has attributes");
xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
//Here goes another way to create nodes.
node = xmlNewNode(NULL, BAD_CAST "node4");
node1 = xmlNewText(BAD_CAST"other way to create content");
xmlAddChild(node, node1);
xmlAddChild(root_node, node); //Dumping document to stdio or file
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1); //free the document
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}*/
/*
gcc -o main xml.c -lxml2 -I /usr/include/libxml2/
下面为创建后产生的结果:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1>content of node1</node1>
<node3 attribute="yes">node has attributes</node3>
<node4>other way to create content</node4>
</root>
下面为在上面基础上执行插入后产生的结果,虽然后面的排列不整齐,但不影响xml的解析。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1>content of node1</node1>
<node3 attribute="yes">node has attributes</node3>
<node4>other way to create content</node4>
<node1>content of node1</node1><node3 attribute="yes">node has attributes</node3><node4>other way to create content</node4><node1>content of node1<node2>node2 is node1's sub</node2></node1><node3 attribute="yes">node has attributes</node3><node4>other way to create content</node4></root>
*/