• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing XML using XPath with XX:ZZ to find

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

If anyone can help then please do so
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Next, you need your XPath to understand what namespace prefixes correspond to which namespace URIs. Here's a link to a tutorial about that which I just tracked down: Java XPath NamespaceContext – NameSpace Resolution Example.

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.
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic