JAVA:使用XPath表达式构建XML文档 [英] JAVA: Build XML document using XPath expressions
问题描述
我知道这不是XPath的真正含义,但如果我有一个XPath表达式的HashMap来评估我将如何构建XML文档。我发现dom-4j的
DocumentHelper.makeElement(branch,xpath)除了它无法创建属性或索引。肯定有一个库可以做到这一点吗?
I know this isn't really what XPath is for but if I have a HashMap of XPath expressions to values how would I go about building an XML document. I've found dom-4j's DocumentHelper.makeElement(branch, xpath) except it is incapable of creating attributes or indexing. Surely a library exists that can do this?
Map xMap = new HashMap();
xMap.put("root/entity/@att", "fooattrib");
xMap.put("root/array[0]/ele/@att", "barattrib");
xMap.put("root/array[0]/ele", "barelement");
xMap.put("root/array[1]/ele", "zoobelement");
会导致:
<root>
<entity att="fooattrib"/>
<array><ele att="barattrib">barelement</ele></array>
<array><ele>zoobelement</ele></array>
</root>
推荐答案
我也想要同样的要求,其中性质如此动态并且不想使用XSLT或任何对象映射框架,所以我已经在java中实现了这个代码并在其上写了博客,请访问,
I also wanted same kind of requirement where nature is so dynamic and dont want to use XSLT or any object mapping frameworks, so i've implemented this code in java and written blog on it please visit,
http://ganesh-kandisa.blogspot.com/2013/08/ dynamic-xml-transformation-in-java.html
或git存储库中的fork代码,
or fork code at git repository,
https://github.com/TheGanesh/DynamicXMLTransformer
这篇关于JAVA:使用XPath表达式构建XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!