The tricky part is, how to work with Namespaces? To get an idea on to the matter consider the following XML document.

The following code fragment will retrieve the ns1:c1 element and the two ns2:color attributes of each element.
// root is the document
OMElement root = builder.getDocumentElement();
AXIOMXPath xpath = new AXIOMXPath("//a:c1");
xpath.addNamespace("a", "http://namespace1.com");
OMElement c1 = (OMElement)xpath.selectSingleNode(root);
System.out.println(c1);
xpath = new AXIOMXPath("//@b:color");
xpath.addNamespace("b", "http://namespace2.com");
List
System.out.println(colors.get(0).getAttributeValue());
System.out.println(colors.get(1).getAttributeValue());
The important line in this code is the xpath.addNamespace(prefix, uri) method. This prefix doesn't have to be the exact prefix used in the actual document (which in fact is not known in most cases).
That's it, have fun with Axiom :)
Aren't you missing some casts to OMElement?
ReplyDeleteAh! thanks for the point.
ReplyDeleteThanks, this was very helpful
ReplyDeleteThis was very help to me at a very critical time :)
ReplyDeleteThanks Saliya!!!
This is a great blog post! Just helped me save lots of time. It's amazing how useful this blog post is even it's as old as 2008!
ReplyDeleteIt really made my day.Thanks Saliya!.
ReplyDelete