Hi..
What I am trying to do here is -
1. Hit a server and receive an XML response.
2. Parse the XML response and store the values of the xml tags in some variables.
I am doing it as – (if “cur” is my root node)
cur1 = cur->children->next;
tagValue = (char*)cur1->children->content;
This gives the value “xyz” in the variable “tagValue” if following is the XML received from the server :
<root>
<child>xyz</child>
</root>
My problem here is since the response is dependent on the input send it is random, it may or may not have comment nodes at places.
If for some input I get a response as -
<root>
<!--This is a sample response-->
<child>xyz</child>
</root>
My code fails. I am clueless what to do.
[edit]
I am using the following code snippet -
xmlParserCtxtPtr ptrCtxt;
xmlDocPtr ptrDoc;
xmlNodePtr ptrCurNode ;
char* ptrTagname = NULL;
ptrCtxt = xmlNewParserCtxt();
ptrDoc = xmlCtxtReadFile(ptrCtxt, ptrsFileName, NULL, XML_PARSE_RECOVER);
if(ptrCtxt->valid != 0)
{
ptrCurNode = xmlDocGetRootElement(ptrDoc);
while(ptrCurNode->children->next != NULL)
{
ptrTagname = (char*)ptrCurNode->children->next->name;
if(strcmp(ptrTagname,"Version")== 0)
{
msptrVersion = (char*)ptrCurNode->children->next->children->content;
}
else if(strcmp(ptrTagname,"Url) == 0)
{
msptrserverURL = (char*)ptrCurNode->children->next->children->content;
}
ptrCurNode->children->next = ptrCurNode->children->next->next->next;
}
}
This works fine if no comment nodes are present as
ptrCurNode->children->next->children->content; will have some values
<version>1.2
But in case of comment nodes, It gives me segmentation fault while accessing
ptrCurNode->children->next->children->content;
as it is pointing to a comment node and
children->content
doesn’t points to any value.
[/edit]