2016-03-03 5 views
0

Я хочу создать DOM для xml. Наконец, DOM должен быть назначен следующему указателю.Как создать DOM с помощью libxml2 в cpp

xmlDocPtr pDOM; 

Я хочу пример cpp. Помогите мне.

Образец xml следующим образом.

<root> 
    <a> 
    <b attr="I want to get this"> </b> 
    </a> 
</root> 
+0

Просто прочитайте документы для libxml2. http://www.xmlsoft.org/examples/parse3.c –

ответ

3

Есть примеры кода на веб-сайте Libxml2 в:

Libxml2 set of examples

В частности, обратите внимание на следующие примеры:

tree1.c: Navigates a tree to print element names

/** 
* 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 

tree2.c: Creates a tree

/* 
* 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 <[email protected]> 
* 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 
+0

спасибо. На самом деле я надеюсь на это. http://xmlsoft.org/examples/io2.c –