Matthew Brown wrote:Hi Mike. Welcome to The Ranch!
If you're going to do this using DOM methods, I'd say it's crying out for a recursive approach, which will be able to cope with arbitrary depth in your XML document. Write a method that processes the top level elements, then calls itself with the next level as the input arguments.
But personally, I'd use an XSLT transformation. If you search for "xslt identity transform" you should find plenty of references to an "identity transformation" - a transformation that produces output identical to the input. That's a good starting point - it's relatively simple to modify that to leave everything untouched except attributes.
Thanks Matthew, XSLT Transforms looking like they might be the way to go. I wanted to do this a quick and dirty way, but nothing is ever quick (although often dirty
)
I tried making the method recursive, but I have to explicitly define which element I'm currently updating in order to strip out the attributes:
(* this line: thisDoc.getChildNodes().item(i).getAttributes().removeNamedItem(childAttribute.getNodeName()))
The rest of it is just repetition. I wrote it like that, in the hope I would be able to later re-write it with one block re-cursively feeding in the NodeList (for a given depth)
The problem is, if you want to removeNameItem from the Document, you have to explicitly define which node you are currently updating(theres no way of searching though the entire depths of the Document).
So as you can see, that line (*) above keeps getting longer, making it not possible to re-cursively do this
(or at least not do it the way I've written it)
I've been banging my head against the wall for a couple of weeks now, it might be time to give up the ghost and stop trying to do this the quick way