Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vijay Bhuruk
Ranch Hand
Posts: 31
MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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>

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 13056
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks g tsuji.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic