add comment programmatically to xml
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开发者_JAVA百科 the employee John should be able to add comment.
any idea? I am using jdom parser.
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);
You can create new comment nodes using Document.createComment() and insert them into your DOM tree using e.g. Node.insertBefore(Node, Node).
精彩评论