This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Joe Ess
  • salvin francis

Parsing XML using XPath with XX:ZZ to find

Posts: 13
  • Mark post as helpful
  • send pies
  • 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
Posts: 24853
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
Screaming fools! It's nothing more than a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!