Reputation: 167
OK, i've been going round in circles with this one. I'm trying to parse an XML file / data into some structs.
Because every child element requires certain business logic to be carried out, I have decided to create separate procedures to manage each child element.
The original xml can be found here: http://hearme.fm/ars.xml
I can manage the DAAST element, this then feeds through to the Ad element, but when I try to get into the Inline element. node->name returns: "Text" and I can't seem to get any further than this.
Can anyone shed any light?
Thanks
#ifndef DAASTXML_C_
#define DAASTXML_C_
#define XMLSTR(str) ((xmlChar *)(str))
#include "daastXML.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <stdio.h>
#include <stdlib.h>
void parseAds(xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile);
void parseInline(xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile);
void parseCreatives(xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile);
void daastParser (char *filePath){
xmlDocPtr doc;
xmlNodePtr node;
doc = xmlParseFile(filePath);
node = xmlDocGetRootElement(doc);
struct daastXML xmlFile;
if (xmlStrcmp (node->name, XMLSTR("DAAST")) == 0){
xmlFile.version = (char *)xmlGetProp(node, XMLSTR("version"));
parseAds(doc, node->children, xmlFile);
}
xmlFreeDoc(doc);
}
void parseAds(xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile){
vectorAds_init(&xmlFile.Ads);
do {
if (node == NULL) break;
if (xmlIsBlankNode(node)) continue;
if (xmlStrcmp (node->name, XMLSTR("Ad")) == 0) {
struct daastAd newAd;
newAd.id = (char *)xmlGetProp(node, XMLSTR("id"));
newAd.sequence = (char *)xmlGetProp(node, XMLSTR("sequence"));
// At this point we need to get the inline (or wrapper) info *** WRAPPER NOT INTEGRATED ***
printf("\nAdvert ");
printf("\n");
parseInline (doc, node->children, xmlFile);
vectorAds_append(&xmlFile.Ads, newAd);
}
} while ((node = node->next));
}
void parseInline (xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile){
printf("\nInline : node name: ");
printf(node->name);
if (xmlStrcmp (node->name, XMLSTR("InLine")) == 0){
printf("We've got the inline element");
}
parseCreatives (doc, node->children, xmlFile);
}
void parseCreatives(xmlDocPtr doc, xmlNodePtr node, struct daastXML xmlFile){
printf("\nCreatives \n");
}
Upvotes: 0
Views: 189
Reputation: 167
It seems that I've found answer, with the help of @kaylum.
I've added the following:
I've added this routine to the parseInline process, and seems to work now
do {
if (node->type == XML_ELEMENT_NODE){
printf(node->name);
break;
}
} while((node = node->next));
i.e. it loops through all of the children for the parent, and I can operate on the node when it returns an Element_Node.
Upvotes: 2