JAVA:使用 XPath 表达式构建 XML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/306863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA: Build XML document using XPath expressions
提问by
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?
我知道这并不是 XPath 的真正用途,但是如果我有一个 XPath 表达式的 HashMap 到值,我将如何构建 XML 文档。我发现 dom-4j 的 DocumentHelper.makeElement(branch, xpath) 除了它不能创建属性或索引。肯定存在可以做到这一点的图书馆吗?
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");
would result in:
会导致:
<root>
<entity att="fooattrib"/>
<array><ele att="barattrib">barelement</ele></array>
<array><ele>zoobelement</ele></array>
</root>
回答by jamesh
I looked for something similar a few years ago - a sort of writeable XPath. In the end, having not found anything, I hacked up something which essentially built up the XML document by adding new nodes to parentexpressions:
几年前我寻找过类似的东西 - 一种可写的 XPath。最后,没有找到任何东西,我修改了一些东西,通过向父表达式添加新节点来构建 XML 文档:
parent="/" element="root"
parent="/root" element="entity"
parent="/root/entity" attribute="att" value="fooattrib"
parent="/root" element="array"
parent="/root" element="ele" text="barelement"
(This was itself to be governed by an XML configuration file, hence the appearance of above.)
(这本身由一个 XML 配置文件管理,因此出现上述情况。)
It would be tempting to try an automate some of this to just take the last path element, and make something of it, but I always felt that there were XPath expressions I could write which such a dumbheaded approach would get wrong.
尝试将其中的一些自动化以仅获取最后一个路径元素并对其进行处理是很诱人的,但我始终觉得我可以编写一些 XPath 表达式,而这种愚蠢的方法会出错。
Another approach I considered, though did not implement (the above was "good enough"), was to use the excellent Jaxen to generate elements that did not exist, on the fly if it didn't already exist.
我考虑过的另一种方法,虽然没有实现(上述方法“足够好”),是使用优秀的 Jaxen 生成不存在的元素,如果它不存在,则即时生成。
From the Jaxen FAQ:
来自Jaxen 常见问题解答:
The only thing required is an implementation of the interface org.jaxen.Navigator. Not all of the interface is required, and a default implementation, in the form of org.jaxen.DefaultNavigator is also provided.
唯一需要的是接口 org.jaxen.Navigator 的实现。并非所有接口都是必需的,还提供了一个默认实现,格式为 org.jaxen.DefaultNavigator。
The DOMWriterNavigatorwould wrap and existing DOMNavigator, and then use the makeElementmethod if the element didn't exist. However, even with this approach,
you'd probably have to do some pre/post processing of the XPath query for things like attributesand text()functions.
本DOMWriterNavigator想包和现有的DOMNavigator,然后使用makeElement方法,如果该元素不存在。但是,即使使用这种方法,您也可能必须对 XPath 查询进行一些预处理/后处理以获取诸如attributes和text()函数之类的东西。
回答by Z. Cass
The best I was able to come up with is to use a JAXB implementation, which will marshall/unmarshal objects to xml and then I used Dozer (http://dozer.sourceforge.net/documentation/mapbackedproperty.html) to map the xpaths which were keys in a map to the JAXB object method setters.
我能想到的最好的方法是使用 JAXB 实现,它将对象编组/解组到 xml,然后我使用 Dozer (http://dozer.sourceforge.net/documentation/mapbackedproperty.html) 来映射 xpath它们是 JAXB 对象方法设置器映射中的键。
<mapping type="one-way" map-id="TC1">
<class-a>java.util.Map</class-a>
<class-b>org.example.Foo</class-b>
<field>
<a key="root/entity/@att">this</a>
<b>Foo.entity.att</b>
<a-hint>java.lang.String</a-hint>
</field>
It's more of a two step solution, but really worked for me.
这更像是一个两步解决方案,但确实对我有用。
回答by Ganesh Kandisa
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,
我也想要同样的需求,其中自然是如此动态,不想使用 XSLT 或任何对象映射框架,所以我已经在 Java 中实现了这段代码并写了关于它的博客,请访问,
http://ganesh-kandisa.blogspot.com/2013/08/dynamic-xml-transformation-in-java.html
http://ganesh-kandisa.blogspot.com/2013/08/dynamic-xml-transformation-in-java.html
or fork code at git repository,
或在 git 存储库中 fork 代码,

