How to bind a xml element into an object member variable?(如何将 xml 元素绑定到对象成员变量中?)
问题描述
我正在尝试使用 moxy 将 xml 解组为对象.下面是 xml 的示例.
I'm trying to unmarshal an xml to an object using moxy.Below is the sample of the xml.
<root>
<name>
<firstname>value</firstname>
</name>
<address>value of address</address>
</root>
下面是我要映射的类.
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;
}
现在如何获取 XML 中地址标记的值并将其绑定到类地址的地址线变量.
Now how do I get the values of the address tag in XML and bind it to the addressline variable of class Address.
推荐答案
您需要在 addressline 属性上使用 @XmlValue 注释.
You need to use the @XmlValue annotation on the addressline property.
@XmlAccessorType(XmlAccessType.FIELD)
class Address {
@XmlValue
String addressline;
}
这篇关于如何将 xml 元素绑定到对象成员变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 xml 元素绑定到对象成员变量中?
基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
