whotheman
whotheman

Reputation: 117

Nodes return empty with libXML2

I've extracted this code (as working) from a project. As I am not knowledgeable about libXML or XML I can't find out the problem to solve. The XML file is here link

I get output:

Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Bolge"
Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Peryot"
Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/İli"
Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Durum"
Error: empty set as a result of evaluation xpath expression "/SOA/sehirler[0]/Mak"
Segmentation fault

As opposed to just getting

İstanbul

in the console output.

Can be compiled with this

gcc -Wall main.c -o out $(pkg-config --cflags --libs glib-2.0 libxml-2.0)

The following is working code to be pasted into the main.c

#include <glib-2.0/glib.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

typedef struct weatherXmlSourceNode {
  char *bolge, *peryot, *ili, *durum;
  int mak;
} weatherSource;
weatherSource *wsrc[6];

xmlDocPtr doc;

char *grabNodeFromFile(xmlDocPtr xml_doc, char* nodeName, int item_index) {
  char xpath[64];
  xmlXPathContextPtr xpathCtx; 
  xmlXPathObjectPtr xpathObj; 
  xmlNodeSetPtr nodeset;

  sprintf(xpath, "/SOA/sehirler[%d]/%s",item_index,nodeName);

  xpathCtx = xmlXPathNewContext(xml_doc);

  if(xpathCtx == NULL) {
    fprintf(stderr,"Error: unable to create new XPath context\n");
    return(NULL);
  }

  /* Evaluate xpath expression */
  xpathObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCtx);
  xmlXPathFreeContext(xpathCtx);
  if(xpathObj == NULL) {
    fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath);
    return(NULL);
  }

  /* Check if xmlXPathEvalExpression had returned nonempty nodeset*/
  if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
    fprintf(stderr,"Error: empty set as a result of evaluation xpath expression \"%s\"",xpath);
    xmlXPathFreeObject (xpathObj);
    return(NULL);
  }

  nodeset = xpathObj->nodesetval;

  //there should be only one node in nodeset. So we return contents of the first node in nodeset
  return (char *)xmlNodeListGetString(xml_doc, nodeset->nodeTab[0]->xmlChildrenNode,1);
}

void fill_entry_from_rss(weatherSource *wsrc, xmlDocPtr filename, int doc_index) {
  /* Fill respective elements */
  wsrc->bolge = grabNodeFromFile(filename, "Bolge", doc_index);
  wsrc->peryot = grabNodeFromFile(filename, "Peryot", doc_index);
  wsrc->ili = grabNodeFromFile(filename, "İli", doc_index);
  wsrc->durum = grabNodeFromFile(filename, "Durum", doc_index);
  wsrc->mak = grabNodeFromFile(filename, "Mak", doc_index);
}

int fill_entry_from_rss_loop() {
  //int doc_item_index = 1;
  /* Load XML documents */
  doc = xmlParseFile("sonSOA.xml");
  if (doc == NULL) {
    fprintf(stderr, "Error: unable to parse file \"sonSOA.xml\"\n");
    return(0);
  }
  /* Allocate memory for weather struct */
  int num = 6;
  while (num > 0) {
    wsrc[(num - 1)] = g_new0(weatherSource, 1);
    num--;
  }
  /* Fill rss entries from their respective fields */
  fill_entry_from_rss(wsrc[0], doc, 0);
  printf("%s\n", wsrc[0]->ili);
  xmlFreeDoc(doc);
  return 1;
}
int main() {
  fill_entry_from_rss_loop();
  return 0;
}

Upvotes: 0

Views: 1848

Answers (1)

whotheman
whotheman

Reputation: 117

Answering my own question, 2 mistakes were present in this code. First of all, xml nodes start with 1 rather than 0 when listed. Also "ili" property from the xml was mistyped as "İli" in my code, so that's fixed too. Working code as follows.

#include <glib-2.0/glib.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

typedef struct weatherXmlSourceNode {
  char *bolge, *peryot, *ili, *durum;
  int mak;
} weatherSource;
weatherSource *wsrc[6];

xmlDocPtr doc;

char *grabNodeFromFile(xmlDocPtr xml_doc, char* nodeName, int item_index) {
  char xpath[64];
  xmlXPathContextPtr xpathCtx; 
  xmlXPathObjectPtr xpathObj; 
  xmlNodeSetPtr nodeset;

  sprintf(xpath, "/SOA/sehirler[%d]/%s",item_index,nodeName);

  xpathCtx = xmlXPathNewContext(xml_doc);

  if(xpathCtx == NULL) {
    fprintf(stderr,"Error: unable to create new XPath context\n");
    return(NULL);
  }

  /* Evaluate xpath expression */
  xpathObj = xmlXPathEvalExpression((xmlChar *)xpath, xpathCtx);
  xmlXPathFreeContext(xpathCtx);
  if(xpathObj == NULL) {
    fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpath);
    return(NULL);
  }

  /* Check if xmlXPathEvalExpression had returned nonempty nodeset*/
  if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) {
    fprintf(stderr,"Error: empty set as a result of evaluation xpath expression \"%s\"",xpath);
    xmlXPathFreeObject (xpathObj);
    return(NULL);
  }

  nodeset = xpathObj->nodesetval;

  //there should be only one node in nodeset. So we return contents of the first node in nodeset
  return (char *)xmlNodeListGetString(xml_doc, nodeset->nodeTab[0]->xmlChildrenNode,1);
}

void fill_entry_from_rss(weatherSource *wsrc, xmlDocPtr filename, int doc_index) {
  /* Fill respective elements */
  wsrc->bolge = grabNodeFromFile(filename, "Bolge", doc_index);
  wsrc->peryot = grabNodeFromFile(filename, "Peryot", doc_index);
  wsrc->ili = grabNodeFromFile(filename, "ili", doc_index);
  wsrc->durum = grabNodeFromFile(filename, "Durum", doc_index);
  wsrc->mak = grabNodeFromFile(filename, "Mak", doc_index);
}

int fill_entry_from_rss_loop() {
  //int doc_item_index = 1;
  /* Load XML documents */
  doc = xmlParseFile("sonSOA.xml");
  if (doc == NULL) {
    fprintf(stderr, "Error: unable to parse file \"sonSOA.xml\"\n");
    return(0);
  }
  /* Allocate memory for weather struct */
  int num = 6;
  while (num > 0) {
    wsrc[(num - 1)] = g_new0(weatherSource, 1);
    num--;
  }
  /* Fill rss entries from their respective fields */
  fill_entry_from_rss(wsrc[0], doc, 1);
  printf("%s\n", wsrc[0]->ili);
  xmlFreeDoc(doc);
  return 1;
}
int main() {
  fill_entry_from_rss_loop();
  return 0;
}

Upvotes: 1

Related Questions