Developing bada: XML Parsing with Namespaces

4 Comments

Thanks to wit’s very helpful introduction into working with XML: Working with Working with XML in Bada (part 1) – Simple XPath Parsing I could quickly enter the quite more sophisticated data processing, but the tutorial reached its bounds when I encountered an XML file with namespaces, which alot xml feeds contain.

Thanks to an overwhelming and impenetrable amount of documentation, I found the solution after long sessions of trial-and-error.

We got a very simple XML file

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.example.com/new">
   <section1>Sec_1</section1>
   <section2>
      <sub>Sub_Sect_2</sub>
   </section2>
</book>

Notice the namespace declared with xmlns="http://www.example.com/new" in the <book> tag? In this case the namespace has no prefix but still all child-tags are included in this namespace. Therefore the XPath Expression //section1 will fail to retrieve the <section1> tag and instead returns NULL.

Would the namespace actually be declared like this:
xmlns:my_ns="http://www.example.com/new"
We would be able to reach the included tags with the XPath Expression:
//my_ns:section1 as the namespace has been registered to an identifier, in this case “my_ns”. Our aim is to do this manually.

Manually register Namespace to Identifier

Thanks to the geeks over at Stackoverflow.com who found the solution to this:
We need to manually register a Namespace within the given XML Context after we initialized the XPath Context itself:

// Initializing Context with the XML file "doc.xml"
xpathCtx = xmlXPathNewContext(doc);
// Registering the Namespace "my_ns" with the given URL
xmlXPathRegisterNs(xpathCtx, (xmlChar*)"my_ns", (xmlChar*)"http://www.example.com/my_ns");

Voilà, now we can finally reach the namespaced tags within our XML document with
//my_ns:section1!

PS: To retrieve the <sub> XML Node as the child of <section2> we need to add the namespace to both tag names: //my_ns:section2/my_ns:sub

4 Comments

Write a comment

Fields marked with * are required.
Don't worry, your email adress will not be displayed or shared with anyone.