• 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

comment a node/element in xml file using java dom parser

 
Ranch Hand
Posts: 31
MyEclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need way to comment out particular node/element in xml file.

what I am doing is
1.parse xml file using dom parser.
2.traverse through child nodes. check node values if it matches to given input value.
3. comment out entire node whose node value==given input.

I can create new comment node and insert or append in document or child. but want to comment already existing node.



here, I want to comment node <title>

 
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
You can't change the Node type in place.

I believe you will have to remove the Node entirely from the Document, then createComment( text ) with the text and finally add it back to the document.

Or possibly you could use the replaceChild( newnode, oldnode ) method.

Bill
 
Vijay Bhuruk
Ranch Hand
Posts: 31
MyEclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, Thanks for reply.

Creating comment node with text seems a only workaround for now.

Node method- getTextContent() returns "Required Information". so newly created comment node will be like

<!-- Required Information -->


is there any way to get text string with node name. like "<tile>Required Information</title>". so that comment will be like

<!-- <title>Required Information </title> -->

 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to do that formatting in your own code.

Note that you can get the name of the node you are replacing with getNodeName() but you will still have to add the formatting "<" etc.

You should get familiar with the excellent table in the JavaDocs for org.w3c.dom.Node - which will help you understand the behavior of the various types of Node implementatiosn.

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
You should be able to do it by writing a specific xslt document to attain the objective. That would provide a solution with better isolation of concerns as you might have loaded the original xml to dom for the purpose of doing something useful other than commenting the title element...
 
Vijay Bhuruk
Ranch Hand
Posts: 31
MyEclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks g tsuji.
 
reply
    Bookmark Topic Watch Topic
  • New Topic