我需要在Spring Boot应用程序中解析这样的XML文档:
Code > Injected language fragment
这是我<Transaction id="{ID}" vs="{VS}" uniq="{UNIQ}">
<Data field1="{VAL1}" field2="{VAL2}" />
</Transaction>
@RestController
和@RequestMapping(value = "/transaction", method = RequestMethod.POST)
public ApiClient transaction(@RequestBody TransactionRequest transactionRequest) {
LOGGER.debug(transactionRequest);
return null;
}
类:
Transaction
和@XmlRootElement(name = "Transaction")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(TransactionRequestData.class)
public class TransactionRequest {
@XmlElement(name = "id")
private long id;
@XmlElement(name = "vs")
private String vs;
@XmlElement(name = "uniq")
private String uniq;
@XmlElement(name = "data")
private TransactionRequestData data;
}
TransactionRequestData
但是这只绑定了@XmlRootElement(name = "Data")
@XmlAccessorType(XmlAccessType.FIELD)
public class TransactionRequestData {
@XmlElement(name = "field1")
private String field1;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
}
个元素数据,但没有绑定Transaction
元素。
有没有办法解析子元素?
答案 0 :(得分:0)
id,vs,uniq属性是你的情况下的xml属性。
使用@XmlAttribute将@XmlElement注释更改为正确的TransactionRequest pojo。
使用以下内容:
@XmlRootElement(name = "Transaction")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(TransactionRequestData.class)
public class TransactionRequest {
@XmlAttribute(name = "id")
private long id;
@XmlAttribute(name = "vs")
private String vs;
@XmlAttribute(name = "uniq")
private String uniq;
@XmlElement(name = "data")
private TransactionRequestData data;
....getters & setters ommited
}
@XmlRootElement(name = "Data")
@XmlAccessorType(XmlAccessType.FIELD)
public class TransactionRequestData {
@XmlAttribute(name = "field1")
private String field1;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
....getters & setters ommited
}
并且不要忘记为房产添加吸气剂和制定者。