[xml] XPath problems.



I am looking at the documentation, and I am stumped...

Now I am sure that there are any number of you out there that will consider
this a stupid premise, but I am missing something.

I *HAD* a bunch of XML functions for working with our applications XML
configuration file that walked the file, but they were buggy and not very
good to begin with.  I am trying to convert them to use XPath.  But I am
missing something.

For example here is my XML_Node_Exists(char *NODE) function (Does not work,
I obviously am not doing something right).

(Note, node is sent: A;B;C for <A><B><C>FOO</C></B></A>)

bool XML_Key_Exists(char *key)
{
        sprintf(tmpstr,"Entering XML_Key_Exists(%s).",key);
        logger(tmpstr,0);
        // Ok, first we have to get our keys..  We will allow 16 levels
deep.

        char *TOKEN=new char[128];
        char *tmpkey=new char[1024];
        char *xpath=new char[1024];
        strcpy(tmpkey,key);
        strcpy(xpath,"");
        TOKEN=strtok(tmpkey,";");
        while(TOKEN!=NULL)
        {
                strcpy(TOKEN,upcase(TOKEN));
                sprintf(xpath,"%s/%s",xpath,TOKEN);
                TOKEN=strtok(NULL,";");
        }
        delete(TOKEN);
        delete(tmpkey);
        
        xmlDocPtr doc;
        xmlXPathContextPtr xpathCtx;
        xmlXPathObjectPtr xpathObj; 


        doc=xmlParseFile(Server_Configuration->Configuration_File);
        if(doc==NULL)
        {
                sprintf(tmpstr,"Cannot open configuration file:
%s.",Server_Configuration->Configuration_File);
                logger(tmpstr,1);
                delete(xpath);
                xmlXPathFreeObject(xpathObj);
                xmlXPathFreeContext(xpathCtx);
                xmlFree(doc);
                return(false);
        }

        xpathCtx=xmlXPathNewContext(doc);
        if(xpathCtx==NULL)
        {
                logger("Unable to create new XPath Context.",1);
                xmlFreeDoc(doc);
                delete(xpath);
                xmlXPathFreeObject(xpathObj);
                xmlXPathFreeContext(xpathCtx);
                xmlFree(doc);
                return(false);
        }
        
        xpathObj=xmlXPathEvalExpression((xmlChar *)xpath,xpathCtx);
        if(xpathObj==NULL)
        {
                logger("Unable to evaluate XPath expression (Key does not
exist?).",1);
                delete(xpath);
                return(false);
        }
        if(xmlXPathNodeSetIsEmpty(xpathObj->nodesetval))
        {
                xmlXPathFreeObject(xpathObj);
                xmlXPathFreeContext(xpathCtx);
                xmlFree(doc);
                delete(xpath);  
                return(false);
        } 
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx);
        xmlFree(doc);
        delete(xpath);  
        return(true);
}

I believe I am evaluating the expression correctly, but I want to know if
the node exists or not (Not if there is data in it).

So for example XML_Node_Exists("a;b;c") should return true for both of the
following:

<a>
<b>
<c>
</c>
</b>
</a>

AND

<a>1<b>2<c>3</c></b></a>

Thanks in advance..
Ron




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]