0
#include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <libxml/tree.h>
23 #include <libxml/parser.h>
24 
25 
26 
27 xmlNodePtr get_child_element_by_name(const xmlNodePtr parent_element, const xmlChar* child_name);
28 
29 int main(int argc, const char *argv[])
30 {
31   const char * xmlPath=argv[1];·
32   xmlDocPtr xmldp=xmlParseFile(xmlPath);
33   if ( !xmldp )
34     return 1;
35 
36   printf("Target xml file: %s\n",xmldp->URL);
37 
38   xmlNodePtr root_element=xmlDocGetRootElement(xmldp);
39 
40   xmlNodePtr tmp=get_child_element_by_name(root_element,"Referenc");
41 
42   if (tmp)
43     printf("found %s\n",tmp->name);
44 
45 
46   return 0;
47 }
48 
49 xmlNodePtr get_child_element_by_name(const xmlNodePtr parent_element, const xmlChar* child_name){
50   if (parent_element == NULL)
51     return NULL;
52 
53   xmlNodePtr child_ele = parent_element->children;
54   xmlNodePtr last_ele = parent_element->last;
55 
56   for (; child_ele; child_ele=child_ele->next){
57     printf("Current child:%s\n",child_ele->name);
58 
59     if (0 == strcmp(child_name, child_ele->name))
60         return child_ele;
61   }
62   return NULL;
63 }

Use this code to parse the following this xml: http://schemas.dmtf.org/ovf/envelope/1/dsp8023_1.0.xsd

the output:

Current child:text
Current child:import
Current child:text
Current child:import
Current child:text
....

Why so many text node? How is text node arranged inside the DOM? I understand that anything inside dom is node and text node is used to store text content of a element. However, this output makes no sense for me. Any helpful insights? Thanks.

4

1 回答 1

0

Every newline and indentation at the end of an <import/> element must be represented as a text node.

于 2013-04-23T02:04:20.053 回答