I'm using Simple Framework for XML serialization/deserialization stuff and while the first is easy, I have issues with the latter. So, I receive XML response from a server and it looks like this:
<?xml version="1.0" ?>
<tables>
<table name="result" a="context" b="name">
<r a="stuff1" b="blahblah" />
</table>
<table name="response" a="error" b="reason">
<r a="0" b="" />
</table>
</tables>
Yes, it has 2 elements with name "table". The catch is that the first "table" element may have more than 3 attributes, which means, I can't just create an universal entity for "table" tag. So, my current code for deserialized entity looks like this:
@Root(name = "tables", strict = false)
public class Response {
@Element(name = "table", required = false)
@Path("//table[@name='result']")
Result resultTable;
@Element(name = "table")
@Path("//table[@name='result']")
Response responseTable;
public Result getResultTable() {
return resultTable;
}
public void setResultTable(Result resultTable) {
this.resultTable = resultTable;
}
public Response getResponseTable() {
return responseTable;
}
public void setResponseTable(Response responseTable) {
this.responseTable = responseTable;
}
}
Unfortunately, it doesn't work: I get an exception:
org.simpleframework.xml.core.PathException: Path '//[@name='result']' in field 'resultTable'
com.package.Response.resultTable references document root
I tried different XPath options like simply wildcarting nodes:
@Path("//*[@name='result']")
@Path("*[@name='result']")
but this didn't work either. So, is it my fault due to incorrect XPath options or limitations of Simple Framework:
One thing to note when using such annotations, is that only a subset of the XPath expression syntax is supported. For example, element and attribute references can not be taken from the root of the document, only references within the current context are allowed.
and should I then do it with other XML deserializers? Thanks.