aspose file tools*
The moose likes XML and Related Technologies and the fly likes Java: removing all of the attributes from XML Document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Java: removing all of the attributes from XML Document" Watch "Java: removing all of the attributes from XML Document" New topic
Author

Java: removing all of the attributes from XML Document

mike pea
Greenhorn

Joined: Apr 02, 2011
Posts: 2
Hi there,

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?

The method I've create so far is as follows:




tenants rights
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

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.
mike pea
Greenhorn

Joined: Apr 02, 2011
Posts: 2
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Wouldn't it be a LOT easier to use SAX processing to create a new attribute stripped file?

Your startElement() event call is the ONLY place that attributes appear, just don't write them to the output file.

Bill
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 541
    
    3
The recursion is not difficult to construct. It is not only shorter, it is conceptually much more satisfactory. This is how it would look like.

You can simply use it and pass to it any Node (an element node - not necessarily the root or even an attribute node or else) and it would eliminate all attributes from the node down (deep-removal).>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java: removing all of the attributes from XML Document