I have the following XML:
version
15.0
&
Using SimpleXML ver: 2.6.6. - without using the annotation @Element(name = "value")
you can do the following:
/**
* User: alfasin
* Date: 8/21/13
*/
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.File;
@Root(name = "rdr", strict = false)
public class SimpleXml {
@Element
@Path("details/detail[1]")
private String name;
@Element
@Path("details/detail[1]")
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static void main(String...args) throws Exception {
String fileName = "C:\\Users\\Nir\\IdeaProjects\\Play\\rdr.xml";
Serializer serializer = new Persister();
File source = new File(fileName);
SimpleXml example = serializer.read(SimpleXml.class, source);
System.out.println(example.getName());
System.out.println(example.getValue());
}
}
OUTPUT:
version
15.0
However, according to the documentation, using XPATH requires that you'll name the variable with the same name that is used in the XML, alas, you can't have two class members sharing the same name ("name and "value"), which means that you cannot use XPATH unless you have only ONE detail
object in the XML.
this is how it would look in xpath: /rdr/details/detail[name='version']/value/text()
so maybe try:
@Path("details/detail[name='version']")
@Element(name = "value")
private String resolution;
see the '' it needs to know this is text
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.