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 元素绑定到对象成员变量中?


- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01