在Java SimpleXML库中设置路径时,如何使用XML元素的值? [英] How do I use the value of an XML element when setting the path in the Java SimpleXML library?
问题描述
我有以下XML:
<rdr>
<details>
<detail>
<name>version</name>
<value>15.0</value>
</detail>
<detail>
<name>resolution</name>
<value>1080X1920</value>
</detail>
</details>
</rdr>
我正在将其编组到以下Java bean中:
and I am marshalling this into the following Java bean:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
@Root(name = "rdr", strict = false)
public class XmlBean {
@Path("details/detail[1]")
@Element(name = "value")
private String version;
@Path("details/detail[2]")
@Element(name = "value")
private String resolution;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
}
我使用简单的XML,版本2.6.6实现此目的: http://simple.sourceforge.net/
I am using simple XML, version 2.6.6 to achieve this: http://simple.sourceforge.net/
但是,我想要做的是使用XPath中的name元素。例如,我不想使用Pathdetails / detail [1],而是使用details / detail [name = version]。这不起作用,我得到一个org.simpleframework.xml.core.PathException:返回路径异常的索引无效。我试过周围的版本'无济于事。根据我对XPath的理解,这是正确的语法: http://www.w3schools.com/ xpath / xpath_syntax.asp 任何人都可以确认这看起来是正确的吗?
However, what I would like to do, is use the name elements in the XPath. For example, rather than using the Path "details/detail[1]" I would like to use "details/detail[name=version]". This doesn't work and I get an org.simpleframework.xml.core.PathException: Invalid index for path exception returned. I have tried surrounding version with ' to no avail. From what I understand of XPath, this is the correct syntax: http://www.w3schools.com/xpath/xpath_syntax.asp Can anyone confirm that this looks correct?
我查看了简单xml库的源代码,它正在处理不使用第三方库本身进行XPath处理。我可以看出为什么它不起作用,因为它只接受数字,并且会为任何不是数字的字符抛出该异常。
I've looked into the source code for the simple xml library and it is handling the XPath processing by itself without using a 3rd party library. I can see why it doesn't work, because it is only accepting digits and will throw that exception for any char that isn't a digit.
我很感动修复库,但怀疑我做错了,可能还有其他注释,我应该使用或其他一些语法。
I'm tempted to fix the library, but suspect that I am doing something wrong and that there may be another annotation that I should be using or some other syntax.
是否有另一个注释我可以用来实现我想要做的事情吗?是否有可能使用Simple做我想做的事情?
Is there another annotation that I can use to achieve what I am trying to do? Is it possible to do what I am trying to do using Simple?
推荐答案
顺便说一句,我通过写自己来解决这个问题图书馆可从此处获取: https://github.com/aembleton/XML-Marshaller
By the way, I resolved this by writing my own library available from here: https://github.com/aembleton/XML-Marshaller
这很简单,基本但当时解决了这个问题。
It's pretty simple and basic but solved the problem at the time.
这篇关于在Java SimpleXML库中设置路径时,如何使用XML元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!