开发者

add comment programmatically to xml

开发者 https://www.devze.com 2023-03-23 14:00 出处:网络
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 c

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消