• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * section:  Tree
3  * synopsis: Creates a tree
4  * purpose:  Shows how to create document, nodes and dump it to stdout or file.
5  * usage:    tree2 <filename>  -Default output: stdout
6  * test:     tree2 > tree2.tmp && diff tree2.tmp $(srcdir)/tree2.res
7  * author:   Lucas Brasilino <brasilino@recife.pe.gov.br>
8  * copy:     see Copyright for the status of this software
9  */
10 
11 #include <stdio.h>
12 #include <libxml/parser.h>
13 #include <libxml/tree.h>
14 
15 #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
16 
17 /*
18  *To compile this file using gcc you can type
19  *gcc `xml2-config --cflags --libs` -o tree2 tree2.c
20  */
21 
22 /* A simple example how to create DOM. Libxml2 automagically
23  * allocates the necessary amount of memory to it.
24 */
25 int
main(int argc,char ** argv)26 main(int argc, char **argv)
27 {
28     xmlDocPtr doc = NULL;       /* document pointer */
29     xmlNodePtr root_node = NULL, node = NULL, node1 = NULL;/* node pointers */
30     char buff[256];
31     int i, j;
32 
33     LIBXML_TEST_VERSION;
34 
35     /*
36      * Creates a new document, a node and set it as a root node
37      */
38     doc = xmlNewDoc(BAD_CAST "1.0");
39     root_node = xmlNewDocNode(doc, NULL, BAD_CAST "root", NULL);
40     xmlDocSetRootElement(doc, root_node);
41 
42     /*
43      * Creates a DTD declaration. Isn't mandatory.
44      */
45     xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
46 
47     /*
48      * xmlNewChild() creates a new node, which is "attached" as child node
49      * of root_node node.
50      */
51     xmlNewChild(root_node, NULL, BAD_CAST "node1",
52                 BAD_CAST "content of node 1");
53     /*
54      * The same as above, but the new child node doesn't have a content
55      */
56     xmlNewChild(root_node, NULL, BAD_CAST "node2", NULL);
57 
58     /*
59      * xmlNewProp() creates attributes, which is "attached" to an node.
60      * It returns xmlAttrPtr, which isn't used here.
61      */
62     node =
63         xmlNewChild(root_node, NULL, BAD_CAST "node3",
64                     BAD_CAST "this node has attributes");
65     xmlNewProp(node, BAD_CAST "attribute", BAD_CAST "yes");
66     xmlNewProp(node, BAD_CAST "foo", BAD_CAST "bar");
67 
68     /*
69      * Here goes another way to create nodes. xmlNewNode() and xmlNewText
70      * creates a node and a text node separately. They are "attached"
71      * by xmlAddChild()
72      */
73     node = xmlNewDocNode(doc, NULL, BAD_CAST "node4", NULL);
74     node1 = xmlNewDocText(doc, BAD_CAST
75                    "other way to create content (which is also a node)");
76     xmlAddChild(node, node1);
77     xmlAddChild(root_node, node);
78 
79     /*
80      * A simple loop that "automates" nodes creation
81      */
82     for (i = 5; i < 7; i++) {
83         sprintf(buff, "node%d", i);
84         node = xmlNewChild(root_node, NULL, BAD_CAST buff, NULL);
85         for (j = 1; j < 4; j++) {
86             sprintf(buff, "node%d%d", i, j);
87             node1 = xmlNewChild(node, NULL, BAD_CAST buff, NULL);
88             xmlNewProp(node1, BAD_CAST "odd", BAD_CAST((j % 2) ? "no" : "yes"));
89         }
90     }
91 
92     /*
93      * Dumping document to stdio or file
94      */
95     xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
96 
97     /*free the document */
98     xmlFreeDoc(doc);
99 
100     /*
101      *Free the global variables that may
102      *have been allocated by the parser.
103      */
104     xmlCleanupParser();
105 
106     /*
107      * this is to debug memory for regression tests
108      */
109     xmlMemoryDump();
110     return(0);
111 }
112 #else
main(void)113 int main(void) {
114     fprintf(stderr, "tree support not compiled in\n");
115     return(0);
116 }
117 #endif
118