How to extract data from XML file

Markco

New Member
I'm new to C programming. I need to do very fast C program which can read XML file using libxml2. This is the XML file which will be read:\[code\]<?xml version="1.0" encoding="UTF-8"?><system-properties version="1.0"> <system-modules> <extensions> <extension module="NetworkModule"/> <extension module="MonitoringModule"/> <extension module="OracleModule"/> <extension module="DataFilterModule"/> </extensions> </system-modules> <system-configuration> <profiles> <profile name="default"> <monitoring-port port="6050"/> <web-service-port port="7050"/> <socket-binding interface="management" ipaddress="192.168.1.101" port="6050"/> <socket-binding interface="monitoring" ipaddress="192.168.1.106" port="7050"/> <network-pool threads="40"/> </profile> </profiles> </system-configuration></system-properties> \[/code\]I tested to write this C code using some libxml2 tutorials which I found on Internet:\[code\]#include <stdio.h>#include <string.h>#include <stdlib.h>#include <libxml/xmlmemory.h>#include <libxml/parser.h>void parseURL (xmlDocPtr doc, xmlNodePtr cur) { xmlChar *key; xmlAttrPtr attr; // Get the childern Element Node of "system-configuration" node cur = cur->xmlChildrenNode; while (cur != NULL) { // check for "profiles" childern element node of "system-configuration" node if ((!xmlStrcmp(cur->name, (const xmlChar *)"profile"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); fprintf(stderr,"profile: %s\n", key); xmlFree(key); // search for "name" attribute attr = xmlHasProp(cur, (const xmlChar*)"name"); /* * Retrieve the value and display it */ key = xmlGetProp(cur, (const xmlChar*)"name"); fprintf(stderr, "name: %s\n", key); xmlFree(key); } // end of IF loop " name" // check for childern element node of "system-configuration" node if ((!xmlStrcmp(cur->name, (const xmlChar *)"monitoring-port"))) { key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); fprintf(stderr,"monitoring-port: %s\n", key); xmlFree(key); } // end of If loop "monitoring-port" cur = cur->next; } // end of While loop return;} // end of parseURL function()/* * Parsing the XML file and Reading the Element Nodes */static void parseDoc(char *xmlFileName) { xmlDocPtr doc; // pointer to parse xml Document xmlNodePtr cur; // node pointer. It interacts with individual node // Parse XML file doc = xmlParseFile(xmlFileName); // Check to see that the document was successfully parsed. if (doc == NULL ) { fprintf(stderr,"Error!. Document is not parsed successfully. \n"); return; } // Retrieve the document's root element. cur = xmlDocGetRootElement(doc); // Check to make sure the document actually contains something if (cur == NULL) { fprintf(stderr,"Document is Empty\n"); xmlFreeDoc(doc); return; } /* We need to make sure the document is the right type. * "system-configuration" is the root type of the documents used in user Config XML file */ if (xmlStrcmp(cur->name, (const xmlChar *) "system-properties")) { fprintf(stderr,"Error"); xmlFreeDoc(doc); return; } /* Get the first child node of cur. * At this point, cur points at the document root, * which is the element "system-configuration" */ cur = cur->xmlChildrenNode; // This loop iterates through the elements that are children of "root" while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar *)"system-configuration"))){ parseURL (doc, cur); } cur = cur->next; } /* Save XML document to the Disk * Otherwise, you changes will not be reflected to the file. * Currently it's only in the memory */ xmlSaveFormatFile (xmlFileName, doc, 1); /*free the document */ xmlFreeDoc(doc); /* * Free the global variables that may * have been allocated by the parser. */ xmlCleanupParser(); return;} // end of XMLParseDoc functionint main(int argc, char **argv) { char *xmlFileName; if (argc <= 1) { printf("Usage: %s inputfile.xml\n", argv[0]); return(0); } // Get the file name from the argv[1] xmlFileName = argv[1]; // Custom function to parse XML file parseDoc (xmlFileName); return (1);}\[/code\]Unfortunately the code is not working. No data is extracted from the XML document. Would you please help me to correct my mistake?Thank you in advance!
 
Back
Top