gpt4 book ai didi

c - xmlNode 路径组件

转载 作者:行者123 更新时间:2023-11-30 17:49:16 28 4
gpt4 key购买 nike

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

谢谢!

最佳答案

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

//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;
}

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

关于c - xmlNode 路径组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967194/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com