2

Is there any way to programmatically comment a particular child in XML? My requirement is I need to find out the attribute value from the xml.If that values exists I need to comment that particular child itself which the attribute belongs. eg:

<Company>
 <employee name="John">
   <dept id="Purchase"></dept>
 </employee>
</company>

so here if I search for dept id "purchase" if it is found then the employee John should be able to add comment.

any idea? I am using jdom parser.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user414967
  • 5,225
  • 10
  • 40
  • 61

2 Answers2

4

This is the JavaCode that will add a comment to the document itself:

Element element = doc.getDocumentElement();
Comment comment = doc.createComment("This is a comment");
element.getParentNode().insertBefore(comment, element);
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
0

You can create new comment nodes using Document.createComment() and insert them into your DOM tree using e.g. Node.insertBefore(Node, Node).

Bombe
  • 81,643
  • 20
  • 123
  • 127