[英]How to bind a xml element into an object member variable?
I'm trying to unmarshal an xml to an object using moxy.Below is the sample of the xml. 我正在尝试使用moxy将xml解组为对象.Below是xml的示例。
<root>
<name>
<firstname>value</firstname>
</name>
<address>value of address</address>
</root>
And below is the class I'm trying to map. 以下是我想要映射的课程。
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlPath("name/firstname/text()")
String name;
Address address;
}
class Address {
String addressline;
}
Now how do I get the values of the address tag in XML and bind it to the addressline variable of class Address. 现在,我如何获取XML中的地址标记的值并将其绑定到类Address的地址行变量。
You need to use the @XmlValue
annotation on the addressline
property. 您需要在
addressline
属性上使用@XmlValue
注释。
@XmlAccessorType(XmlAccessType.FIELD)
class Address {
@XmlValue
String addressline;
}
this is an answer for a similar (but not exactly the same) question that was linked here: 这是一个类似(但不完全相同 )的问题的答案:
The solution for our issue relates to this question as well. 我们问题的解决方案也与此问题有关。 For the issue above, the short answer ( as noted there ) is to use @ XmlValue attribute with the getMessageText(), not @XmlElement.
对于上面的问题,简单的答案( 如指出 存在 )是使用@与getMessageText(XmlValue属性),而不是@XmlElement。 I had already use 'XmlValue', and it still didn't work, so I reverted to XmlElement.
我已经使用'XmlValue'了,它仍然没有用,所以我还原到了XmlElement。
XmlValue wasn't the whole solution in that case . 在这种情况下, XmlValue不是整个解决方案。 We also found that we need:
我们还发现我们需要:
Apparently because of other stuff in the class. 显然是因为课堂上的其他东西。 Evidentially JABX tries to match every get/set property with XML and apparently it got confused and can't or won't process my XmlValue if there are non-XML POJO properties (I deduce).
显然,JABX尝试将每个get / set属性与XML匹配,显然它很混乱,如果存在非XML POJO属性(我推断),则不能或不会处理我的XmlValue。
@XmlAccessorType( XmlAccessType.NONE )
@XmlRootElement(name = "announcement")
public class Announcement
{
...
@XmlValue
public String getMessageText(){
return this.messageText;
}
}
Lesson learned. 学过的知识。 If your JAXB doesn't do what you think it ought to do, I have confused it .
如果你的JAXB没有做你认为它应该做的事情, 我就把它搞糊涂了 。 Thanks for the help, knowing what is needed to do, let us find what else we needed too.
感谢您的帮助,知道需要做什么,让我们找到我们还需要的其他东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.