[英]Jaxb - how do i unmarshal one xml element to multiple fields?
xml: xml:
<root> <element>value (something)</element> </root>
I need to unmarshall this xml to two different fields in my java class.我需要将这个 xml 解组到我的 java class 中的两个不同字段。
I tried the following but it didn't work.我尝试了以下但没有奏效。 the first field had the right value and the second one was null.
第一个字段具有正确的值,第二个字段是 null。
java: java:
@XmlElement(name="element")
@XmlJavaTypeAdapter(TakeValueBeforeParentheses.class)
private String one;
@XmlElement(name="element")
@XmlJavaTypeAdapter(TakeValueInParentheses.class)
private String two;
How can I achieve this without creating a class to hold both values and map that to "element"?如何在不创建 class 来保存两个值和 map 到“元素”的情况下实现这一点?
You may annotate setters and not fields, and then make setter parse value to two fields:您可以注释 setter 而不是字段,然后使 setter 将值解析为两个字段:
@XmlElement(name="element")
void setElement(String value) {
// parse value
StringTokenizer st = new StringTokenizer(value, "()");
this.one = st.nextToken().trim();
this.two = st.nextToken().trim();
}
i think it's a bad practice, because the unmarchalling system play with markers separators only.我认为这是一种不好的做法,因为无与伦比的系统仅使用标记分隔符。 You can split the line after unmarchalling the xml.
您可以在取消编组 xml 后拆分线路。
Why not use this xml format:为什么不使用这个 xml 格式:
<root><elements><element>value</element><element>something</element></elements></root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.