Search code examples
stringfileparsingxpathbada

Parse XML from String using XPath in Bada?


I have read the tutorial on XML parsing in Bada. But I don't want to use a file. I need to parse my XML from a Osp::Base::String. Any ideas which methods should I use? So far I have replaced

xpathCtx = xmlXPathNewContext(doc);
if(xpathCtx == NULL) {
   AppLog("Error: unable to create new XPath context");
   xmlFreeDoc(doc);
   return(E_IO);
}

with

xpathCtx = (xmlXPathContextPtr) xmlXPathNewCString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><title lang=\"en\">XQuery Kick Start</title><title lang=\"en\">Learning XML</title>");

But the emulator simply closes.

Thank you.


Solution

  • Your code is wrong because you cannot cast from xmlXPathObjectPtr to xmlXPathContextPtr: they are different structs.

    The code given in the tutorial is right, just use

    xmlDocPtr xmlReadDoc (const xmlChar * cur, const char * URL, const char * encoding, int options)

    instead of

    xmlReadFile(..)

    To understand how to use this function have a look at the docs and at the examples on libXML2 website.