Libxml2 cannot receive content from node

I use libxml in C, and so I create xml:

xmlDocPtr createXmlSegment(char *headerContent, char *dataContent) { xmlDocPtr doc; doc = xmlNewDoc(BAD_CAST "1.0"); xmlNodePtr rdt, header, data; rdt = xmlNewNode(NULL, BAD_CAST "rdt-segment"); xmlSetProp(rdt, "id", "1"); header = xmlNewNode(NULL,BAD_CAST "header"); data = xmlNewNode(NULL, BAD_CAST "data"); xmlNodeSetContent(header, BAD_CAST headerContent); xmlNodeSetContent(data, BAD_CAST dataContent); xmlAddChild(rdt, header); xmlAddChild(rdt, data); xmlDocSetRootElement(doc, rdt); return doc; } 

and that’s exactly how I want to get data from this xml:

 int getDataFromXmlSegment(char *data, char *header, char *content) { xmlDocPtr doc = xmlReadMemory(data, strlen(data), NULL, NULL, XML_PARSE_NOBLANKS); xmlNode *rdt = doc->children; xmlNode *headerNode = rdt->children; header = (char *)headerNode->content; content = (char *)headerNode->next->content; printf("header: %s, content: %s", header, content); return EXIT_SUCCESS; } 

When I check the title name of Node-> name or β†’ next->, the names are correct (these are the names of these elements), but the content returns null. Does anyone know where the problem is?

+7
source share
1 answer

Short answer: use xmlNodeGetContent .

Element nodes themselves do not contain content. Instead, they have child text nodes, and those that contain content. The content of an element can be a combination of text and tags, which allows it to maintain order, represent objects, etc.

You can iterate over child nodes and view elements of THEIR content, but xmlNodeGetContent does this for you and correctly processes child tags and objects.

+11
source

All Articles