[Simple-support] How do I use the value of an XML element when setting the path?
Brought to you by:
niallg
|
From: Arthur E. <ar...@bl...> - 2012-09-06 16:51:38
|
I've posted this question onto Stackoverflow, but thought I should post it here to see if anyone has an answer: http://stackoverflow.com/questions/12298673/how-do-i-use-the-value-of-an-xml-element-when-setting-the-path-in-the-java-simpl I have the following XML: <rdr> <details> <detail> <name>version</name> <value>15.0</value> </detail> <detail> <name>resolution</name> <value>1080X1920</value> </detail> </details> </rdr> 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; } } I am using simple XML, version 2.6.6. 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? 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. 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? |