On 8/27/10 7:11 PM, Face wrote:
Hello all,I am trying to move from .Net System.Xml to libxml2 and i would like to know how can i print the element markup representing of the current node and all its child nodes using libxml2. coming from .Net background i could do this by System.Xml.XmlNode.OuterXml of the current node. I tried to read the document on xmlsoft.org however i is very confusing to me . therefore, if anyone could help me with this or point me to the right direction here what i did so far: <?xml version="1.0"?> <story> <storyinfo> <author>John Fleck</author> <datewritten>June 2, 2002</datewritten> <keyword>example keyword</keyword> </storyinfo> <body> <headline>This is the headline</headline> <para>This is the body text.</para> </body> </story> #include<stdio.h> #include<libxml/parser.h> #include<libxml/tree.h> static void print_element_names(xmlNode * a_node) { xmlNode *cur_node = NULL; xmlAttr *cur_attr = NULL; for (cur_node = a_node; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { printf("name: %s\n", cur_node->name); for (cur_attr = cur_node->properties; cur_attr; cur_attr = cur_attr->next) { printf("\tattribute : %s \t%s\n", cur_attr->name, cur_attr->children->content); } } if (cur_node->type == XML_TEXT_NODE) { printf("\tcontent: %s\n", cur_node->content); } print_element_names(cur_node->children); char Staring[1]="s"; } } int main(int argc, char **argv) { xmlDoc *doc = NULL; xmlNode *root_element = NULL; if (argc != 2) return(1); LIBXML_TEST_VERSION // Macro to check API for match with // the DLL we are using /*parse the file and get the DOM */ if ((doc = xmlReadFile(argv[1], NULL, 0)) == NULL){ printf("error: could not parse file %s\n", argv[1]); exit(-1); } /*Get the root element node */ root_element = xmlDocGetRootElement(doc); print_element_names(root_element); xmlFreeDoc(doc); // free document xmlCleanupParser(); // Free globals return 0; } ./test Sample.xml =========================================== name: story content: name: storyinfo content: name: author content: John Fleck content: name: datewritten content: June 2, 2002 content: name: keyword content: example keyword content: content: name: body content: name: headline content: This is the headline content: name: para content: This is the body text. content: content: =========================================== what i need to print is the element like this <datewritten>June 2, 2002</datewritten>
Take a look at the functions xmlNodeDump and xmlNodeDumpOutput Rob _______________________________________________ xml mailing list, project page http://xmlsoft.org/ [email protected] http://mail.gnome.org/mailman/listinfo/xml
