• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java: removing all of the attributes from XML Document

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).>
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic