• 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

how to remove a child (using JDOM)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my xml file:
<person>
<name>Tom</name>
<age>34</age>
</person>
i want to remove the <age> tag, how could i do by jdom?
Should i read file first and rewrite it?

Best Regards,
Ronyn
[This message has been edited by Ronyn Woods (edited October 31, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  • First parse the document using DOMBuilder. You can use any of overloaded build(..) method to do this. The method returns you a Document object.
    Document doc = DOMBuiler().build(....);
  • Get the root element by calling getRootElement() on the Document object returned. This will return a reference to [/rul=http://www.jdom.org/docs/apidocs/org/jdom/Element.html]Element[/url] object.
    Element root = doc.getRootElement();
  • Once you have the root element, simply call the removeChild(..) method to detach a particular node. Note that this is an overloaded method that supports elements that has an associated namespace. The element has to be the first level child of the parent. If your element is deeply nested, you may have to iteratively( or recursively ) search for it. Instead of calling removeChild(), you can call getChild(...) on the element to get the Element first and then call detach() on element reference. Both has the same effect ie., the particular element being detached from its parent.
  • Now that your DOM tree has changed, you will have to save it to an XML file. Use jdom.output.XMLOutputter to write the DOM tree to an output stream. Since the XMLOutputter takes a JDOM tree and formats it to a stream as XML, you don't have to convert the JDOM tree into a DOM tree.

  • Hope this helps. What I have tried to outline here is the basic process. Of course you are free to make any changes to these steps based on the structure of your document and other business requirements.

    ------------------
    Ajith Kallambella M.
    Sun Certified Programmer for the Java�2 Platform.
    IBM Certified Developer - XML and Related Technologies, V1.
    Co-author of Java 2 Certification Passport
 
Ronyn Woods
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Does anybody used JDOM to retrieve particular element tags with Collection classes. Is it flexible enough to retrieve particular attribute values in XML DOM tree using Java Collections. As far as other parsers concerned we need to develop our own algorithm to fetch desired elements into Collection classes.

Anil.
 
reply
    Bookmark Topic Watch Topic
  • New Topic