I want to create a dynamic xml tree base on the xpath. Let's say my xpath value is
Product/Organization/RegisteredDetail/something
and I want to put value in the following format.
<product>
<organization>
<registeredDetail>
<something>valueOfSomething</something>
</registeredDetail>
</organization>
<product>
In short I want to create a treeview/tree table by reading xpath. I want to put the value at the inner most child and show the location of value in a tree structure Or simply the location of the child in a tree form. The value of the xpath will be vary. Any suggestion using java or jquery will be valuable for me.
I tried to implement suggestion given by @ThW. I made some modifications:
var dom = document.implementation.createDocument("", "", null);
var node = dom;
New code:
var pathmap = new Object();
var path1 = 'Product/Organization/RegisteredDetail/something';
var path2 = 'Product/Organization/RegisteredDetail';
var path3 = 'Product/Organization/RegisteredDetail/anything/nothing';
pathmap[path1] = 'Product/Organization/RegisteredDetail/something';
pathmap[path2] = 'Product/Organization/RegisteredDetail';
pathmap[path3] = 'Product/Organization/RegisteredDetail/anything/nothing';
console.log(pathmap);
for (var path in pathmap) {
var parts = path.split(/\//);
for (var i = 0; i < parts.length; i++) {
node = node.appendChild(dom.createElement(parts[i]));
}
node.appendChild(dom.createTextNode('valueOfSomething'));
}
var serializer = new XMLSerializer();
console.log(dom);
and the output I got is
As you can see tree nodes are repeating. I need to add the valueOfSomething where it belongs. If the node exist don't recreate that, simply add a new child. something like this
<product>
<organization>
<registeredDetail>
valueOfSomethng
<something>valueOfSomethng</something>
<anything>
<nothing>valueOfSomethng</nothing>
</anything>
</registeredDetail>
</organization>
</product>
I am planning to put the value of xpath and valueOfSomething in a hashMap. and put the value of 'valueOfSomething' dynamically.