Java: removing all of the attributes from XML Document

I'm currently on the SCJP course (I'm on book 3 of 4 and should take the exam within then next year)

This is question is for a personal project I'm working on, and I've only covered xml breifly in Book 3 (the course recomends this forum )
I have lots of xml files that I want to strip down to a very simplified format (and manuipulate some elements).
As part of this stripping down, I need to remove all of the XML attibutes at some point.

I'm trying to write a method that will strip out all of the attributes from my XML Document (going through each child, child-child ... etc in turn)
The way I'm doing it is clearly wrong, but I havent figured out a better way to do it yet. If anyone could point me in the right direction.
As you can see, I will keep cycling through for ever and this is huge and currently only goes 4 nodes deep, is there a better way?

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.

mike pea
Greenhorn

Joined: Apr 02, 2011
Posts: 2

posted Apr 02, 2011 16:33:42

0

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