Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 -

C++
xmlParserCtxtPtr ptrCtxt; /* the parser context */
 xmlDocPtr ptrDoc; /* the resulting document tree */
 xmlNodePtr ptrCurNode ;
 char* ptrTagname = NULL;
    /* create a parser context */
    ptrCtxt = xmlNewParserCtxt();
     /* parse the file, activating the DTD validation option */
     ptrDoc = xmlCtxtReadFile(ptrCtxt, ptrsFileName, NULL, XML_PARSE_RECOVER);

     /* check if validation suceeded */
     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]
Posted
Updated 27-Nov-13 5:24am
v4
Comments
AsthaS 27-Nov-13 1:46am    
Please help me out with this...anyone...I am really stuck...!!
Richard MacCutchan 27-Nov-13 5:30am    
Check each node as you travers the XML, and ignore it if it's a comment. Your question does not really give us any idea what your problem is.
AsthaS 27-Nov-13 6:25am    
Hi..Thanks for the response. I have improved the question. Hope it can help you help me now...!!
Richard MacCutchan 27-Nov-13 6:42am    
You have not shown any code or expalined what errors you are seeing so we have no way of offering suggestions. How are you parsing the XML and what checks do you make to validate each node?
AsthaS 27-Nov-13 7:12am    
Moved to question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900