java - Creating dynamic xml tree based on the xpath value -
I want to create a dynamic xml tree base on xpath. Assume that my xpath value is
product / organization / registered / something and I want to put the value in the following format. pre & lt; & Lt; Organization & gt; & Lt; RegisteredDetail & gt; & Lt; Some & gt; ValueOfSomething & lt; / Some & gt; & Lt; / RegisterDetail & gt; & Lt; / Organization & gt; & Lt; Products & gt;
In short I want to make a treeview / tree table by reading xpath. I want to value most children in the interior and show the place of value in the structure of a tree or just the location of the child as a tree. The value of the xpath will be different. Any suggestions for using java or jquery will be valuable to me.
I tried to apply the suggestion given by @hhw I made some amendments:
var dom = document.implementation.createDocument ("", "" , tap); Var node = dom; New code:
var pathmap = new object (); Var Path 1 = 'Product / Organization / Registered Account / Something'; Var path2 = 'product / organization / registered' description '; Var Path 3 = 'Products / Organizations / Registered Accounts / Nothing'; Paththap [Path 1] = 'Product / Organization / Registered Account / Something'; Paththap [Path 2] = 'Product / Organization / Registered' Details'; Paththap [Path 3] = 'Products / Organizations / Registered Accounts / Nothing'; Console.log (pathmap); (Path path in pathmap) {var parts = path.split (/ \ //); For (var i = 0; i & lt; parts.length; i ++) {node = node .appendChild (dom.createElement (parts [i])); } Node.appendChild (dom.createTextNode ('valueOfSomething')); } Ser serializer = new XMLSerializer (); Console.log (dome); and I found the output
As you can see that tree nodes are repeating. I have to add that value where it is related if the node is not present, add new child again. Something like this
& lt; Products & gt; & Lt; Organization & gt; & Lt; RegisteredDetail & gt; ValueOfSomethng & lt; Some & gt; Valueoffsmuthing & lt; / Something & gt; & Lt; Anything & gt; & Lt; Nothing & gt; ValueOfSomethng & lt; / Nothing & gt; & Lt; / Anything & gt; & Lt; / RegisterDetail & gt; & Lt; / Organization & gt; & Lt; / Products & gt; I'm planning to add value to xpath and value in something hash map And put value 'valueOfSomething' dynamically.
Except allows for many syntax. The result is not necessary, which can be parsed in the list of element names. For example, any result in / product // some product will select some nodes.
If you have a list of elements then you can easily create nodes from them. XPath can be used to fetch the existing nodes:
function addElementByPath (parent, path, value) {var node = parent; Var parts = path.split (/ \ //); Var dom = parent.ownerDocument; Current node of var; (Var i = 0; i & lt; parts.length; i ++) {currentNode = dom.evaluate (parts [i], node, faucet, XPathResult.FIRST_ORDERED_NODE_TYPE, blank) .singleNodeValue; If (existing node) {node = existing node; } And {node = node .append chalk (dom.createElement (parts [i])); }} Node.appendChild (dom.createTextNode (value)); } Path path = ['product / organization / registered account / something', 'product / organization / registered account', 'product / organization / registered account / nothing', 'some / other path']; Var dom = document.implementation.createDocument ("", "", tap); Var root = dom.appendChild (dom.createElement ('tree')); (Var i = 0; i & lt; paths.length; i ++) {addElementByPath (root, path [i], path [i])} console.dirxml (dom); Output:
& lt; Tree & gt; & Lt; Products & gt; & Lt; Organization & gt; & Lt; RegisteredDetail & gt; & Lt; Some & gt; Product / Organization / RegisteredDetail / Some & lt; / Some & gt; Product / Organization / Registered account & lt; Anything & gt; & Lt; Nothing & gt; Product / Organization / RegisteredDetail / Nothing / None & lt; / Nothing & gt; & Lt; / Anything & gt; & Lt; / RegisteredDetail & gt; & Lt; / Organization & gt; & Lt; / Products & gt; & Lt; Some & gt; & Lt; OtherPath & gt; Some / OtherPath & lt; / OtherPath & gt; & Lt; / Some & gt; & Lt; / Tree & gt;
Comments
Post a Comment