0

我已经从一个项目中提取了这个代码(作为工作)。由于我不了解libXMLXML找不到要解决的问题。文件在XML这里链接

我得到输出:

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

而不是仅仅得到

İstanbul

在控制台输出中。

可以用这个编译

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

以下是要粘贴到 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;
}
4

1 回答 1

1

回答我自己的问题,此代码中存在 2 个错误。首先,xml 节点在列出时以 1 而不是 0 开头。在我的代码中,xml 中的“ili”属性也被错误地输入为“İli”,所以这也是固定的。工作代码如下。

#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;
}
于 2013-08-28T11:19:14.173 回答