gpt4 book ai didi

java - 基于xpath值创建动态xml树

转载 作者:行者123 更新时间:2023-12-01 12:26:27 29 4
gpt4 key购买 nike

我想基于 xpath 创建一个动态 xml 树。假设我的 xpath 值为

Product/Organization/RegisteredDetail/something

我想以以下格式输入值。

<product>
<organization>
<registeredDetail>
<something>valueOfSomething</something>
</registeredDetail>
</organization>
<product>

简而言之,我想通过读取 xpath 创建一个 TreeView /树表。我想将值放在最里面的子节点上,并在树结构中显示值的位置,或者只是以树形式显示子节点的位置。 xpath 的值会有所不同。任何使用 java 或 jquery 的建议对我来说都很有值(value)。

我尝试实现@ThW 给出的建议。我做了一些修改:

var dom = document.implementation.createDocument("", "", null);
var node = dom;

新代码:

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

我得到的输出是 enter image description here

正如您所看到的,树节点是重复的。我需要将 valueOfSomething 添加到它所属的位置。如果该节点存在,则无需重新创建该节点,只需添加一个新的子节点即可。像这样的东西

<product>
<organization>
<registeredDetail>
valueOfSomethng
<something>valueOfSomethng</something>
<anything>
<nothing>valueOfSomethng</nothing>
</anything>
</registeredDetail>
</organization>
</product>

我计划将 xpath 和 valueOfSomething 的值放入 hashMap 中。并动态放置“valueOfSomething”的值。

最佳答案

Xpath 允许使用更多语法。结果不一定是可以解析为元素名称列表的东西。例如/product//something将选择product中的任何后代something节点。

如果您有元素名称列表,您可以轻松地从中创建节点。 XPath 可用于获取现有节点:

function addElementByPath(parent, path, value) {
var node = parent;
var parts = path.split(/\//);
var dom = parent.ownerDocument;
var existingNode;

for (var i = 0; i < parts.length; i++) {
existingNode = dom.evaluate(
parts[i], node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
).singleNodeValue;
if (existingNode) {
node = existingNode;
} else {
node = node.appendChild(dom.createElement(parts[i]));
}
}
node.appendChild(dom.createTextNode(value));
}

var paths = [
'Product/Organization/RegisteredDetail/something',
'Product/Organization/RegisteredDetail',
'Product/Organization/RegisteredDetail/anything/nothing',
'Some/OtherPath'
];

var dom = document.implementation.createDocument("", "", null);
var root = dom.appendChild(dom.createElement('tree'));

for (var i = 0; i < paths.length; i++) {
addElementByPath(root, paths[i], paths[i])
}

console.dirxml(dom);

输出:

<tree>
<Product>
<Organization>
<RegisteredDetail>
<something>Product/Organization/RegisteredDetail/something</something>
Product/Organization/RegisteredDetail
<anything>
<nothing>Product/Organization/RegisteredDetail/anything/nothing</nothing>
</anything>
</RegisteredDetail>
</Organization>
</Product>
<Some>
<OtherPath>Some/OtherPath</OtherPath>
</Some>
</tree>

关于java - 基于xpath值创建动态xml树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26295599/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com