xmlNode路径组件

时间:2013-07-31 09:54:09

标签: c xml libxml2

我正在尝试在libxml2中找到允许我访问xmlNode的路径组件的结构,函数或宏。例如,如果xmlGetNodePath返回/xml/a/b/c[42],我在哪里可以获得位置(42)或b之类的组件?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是您的解决方案(仅当我正确提出您的问题时)。 请检查您的要求。

// XMLParse.cpp:定义控制台应用程序的入口点。 //

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/tree.h>
/*
 * A person record
 */
typedef struct person {
    char *name;
    char *email;
    char *company;
    char *organisation;
    char *smail;
    char *webPage;
    char *phone;
} person, *personPtr;

/*
 * a Description for a Job
 */
typedef struct job {
    char *projectID;
    char *application;
    char *category;
    personPtr contact;
    int nbDevelopers;
    personPtr developers[100]; /* using dynamic alloc is left as an exercise */
} job, *jobPtr;

/*
 * And the code needed to parse it
 */
personPtr parsePerson(xmlDocPtr doc, xmlNodePtr cur) {
    personPtr ret = NULL;

//DEBUG("parsePerson\n");
    /*
     * allocate the struct
     */
    ret = (personPtr) malloc(sizeof(person));
    if (ret == NULL) {
        fprintf(stderr,"out of memory\n");
        return(NULL);
    }
    memset(ret, 0, sizeof(person));

    /* We don't care what the top level element name is */
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
        if ((!strcmp(cur->name, "Person")) )
            ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
        if ((!strcmp(cur->name, "Email")) )
            ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
        cur = cur->next;
    }

    return(ret);
}

/*
 * And the code needed to parse it
 */
jobPtr parseJob(xmlDocPtr doc, xmlNodePtr cur) {
    jobPtr ret = NULL;

//DEBUG("parseJob\n");
    /*
     * allocate the struct
     */
    ret = (jobPtr) malloc(sizeof(job));
    if (ret == NULL) {
        fprintf(stderr,"out of memory\n");
        return(NULL);
    }
    memset(ret, 0, sizeof(job));

    /* We don't care what the top level element name is */
    cur = cur->xmlChildrenNode;
    cur = cur->next;
    cur = cur->xmlChildrenNode;
    cur = cur->next;
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {

        if ((!strcmp(cur->name, "Project")) ) {
            ret->projectID = xmlGetProp(cur, "ID");
            if (ret->projectID == NULL) {
                fprintf(stderr, "Project has no ID\n");
            }
        }
        if ((!strcmp(cur->name, "Application")) )
            ret->application = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
        if ((!strcmp(cur->name, "Category")) )
            ret->category = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
        if ((!strcmp(cur->name, "Contact")) )
            ret->contact = parsePerson(doc,cur);
        cur = cur->next;
    }

    return(ret);
}

int main(int argc, char* argv[])
{
    jobPtr mainret = NULL;
    xmlDocPtr doc;
    xmlNodePtr cur;
    doc = xmlParseFile("D://XMLParser/XMLParser1.0/comman/bin/myxml.xml");
    if (doc == NULL ) {
         fprintf(stderr,"Document not parsed successfully. \n");
         return 0;
    }
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return 0;
    }

    mainret = parseJob(doc,cur);

    xmlFreeDoc(doc);

    return 0;
}

检查一下,如果我错了,请告诉我。