Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Parsing XML using XPath with XX:ZZ to find

Posts: 22
  • 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
Posts: 27379
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.
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:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic