How can I change the namespace on every node in a DOM?

前端 未结 8 676
小蘑菇
小蘑菇 2020-12-19 10:05

How can I, given a w3c DOM (Java\'s default implementation, specifically) change the namespace of every element/attribute/node in that DOM? Efficiently, preferably. The DO

相关标签:
8条回答
  • 2020-12-19 10:52

    If intent is to just change name space, then just use some stream editor to change NS mapping to URL.

    A Namspace is more or less a binding between namespace prefix and a URI. In order to quickly change namespace, just change the mapping:

    Before: xmlns:myNS="my-namespace-uri"

    After: xmlns:myNS="my-new-namespace-uri"

    Basically changing mapping is sufficient, if intent is simply to change the namespace. Moreover if XML Document has default namespace, then changing the default namespace URL value would change namespace for whole of the document.

    Before: xmlns="my-namespace-uri"

    After: xmlns="my-new-namespace-uri"

    0 讨论(0)
  • 2020-12-19 10:55

    Based on my hugely biased opinion what you want will be a huge pain in the ass. I can see the typecasting hell and numerous recursive for loops needed to do this reliably already! Ahh, Java's default implementation, how I hate your NPE:s at internals, the reversed logic, the extra steps needed for simple operations!

    So yes, my suggestion would be recursive for loops with typecasting for every single possible node type, based on my very personal experience the Java's default implementation sucks that badly.

    0 讨论(0)
提交回复
热议问题