The real meat of this solution is the
addElementToParent
method. What it does it searches for the parent element of the provided xpath. If it doesn't find the parent in the document it will create it via a recursive call. Another trick is it will create any siblings that are needed in case the parent has an index (like /root/parent[3]
). Once all the prerequisite elements are created it will then create the element we originally asked for.
This example will create this XML even though we only really tell it to create 5 elements. (the elements depend on the
data
element so it is created automatically.
Hi Brett, did you remove this solution? I was looking for something similar, and could use it to get started!
ReplyDeleteThanks
It still show up for me. Here is a link to the files on github: https://gist.github.com/bmchild/6285705#file-xpathutils-java
ReplyDeleteHi Brett,
ReplyDeleteIt is a nice piece of work. Do you also have a version that handles Attributes??
Thanks
Please post if this works in the case of attributes.It worked for the elements.Any pointers would be of great help
ReplyDelete