[xml] Problem while parsing XML code with libxml2



Hi all
Hope this is the right place where to post a problem with the libxml2 library.
This is my first experience with libxml2.

I'm writing a C program that performs a query to a PostgreSQL database in order to get an XML code.
After that I must to parse the XML results to get some information.

This is a sample of the XML returned from the query:
<gmd:MD_Metadata xmlns:gmd="http://www.isotc211.org/2005/gmd"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:gml="http://www.opengis.net/gml"; xmlns:gts="http://www.isotc211.org/2005/gts"; xmlns:gco="http://www.isotc211.org/2005/gco";>
<gmd:fileIdentifier>
<gco:CharacterString xmlns:srv="http://www.isotc211.org/2005/srv"; xmlns:gmx="http://www.isotc211.org/2005/gmx";>8a0bbb8e-6bf0-45d7-9401-0795ed64b1f6</gco:CharacterString>
</gmd:fileIdentifier>

...

And there is a the code I'm running to parse the XML
(assume that the XML is available in a char *)

int main(int argc, char* argv[]) {
    ...
    char *results     // results coming from the PostgreSQL database query
    const xmlChar *xpathExpr;

    xmlXPathObjectPtr xpathObj;
    xmlXPathContextPtr xpathCtx;
    xmlDocPtr doc;

    ...

xpathExpr = (xmlChar*) "/MD_Metadata/fileIdentifier/CharacterString";

xmlInitParser();

doc = xmlParseMemory (results, strlen(results));
xpathCtx = xmlXPathNewContext(doc);

xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval))
    exit(3)

...

}


The program exit with code 3, so there are no results while parsing the XML.
I also tried to writ the XML to a file, open and parse it with the xmlParseFile function but the result is the same.

The path exists and I think it is correct since using the "gmd:" prefix the xmlXPathEvalExpression function seg faults.

I cannot understand what is wrong i my code.
Hope that someone can answer to my question.

Thanks.

--

Giacomo Piva






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