Hey all I am looking to find a value using XPath and once found, replace that value with something else that I define.
Problem being is that my XML file has levels like:
I have not been able to find any examples that contain the above - mainly the ic:, jxdm:....
The code I scraped together is:
I would be open to using another framework if needed (and also makes this easier) but I'm trying to use what java already has included out of the box. And if there's something other than XPath that would also make this easier then please let me know.
Your input document uses namespaces, so your code needs to take that into account.
First, you need a DocumentBuiderFactory which is namespace-aware. By default they aren't. So call the setNamespaceAware(true) method on your DocumentBuiderFactory when you create it. That way the DocumentBuilder which your DocumentBuiderFactory creates will handle namespaces properly.
Hopefully that gets you on the right track. On the other hand, it looks like your code is just copying the input document to the output document while changing a few values on the way. If it were me I would write an XSLT transformation which did that, rather than your Java solution. It's just a modification of the standard identity transformation. That may or may not be a better route for you.
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad: