java - 如何使用 java 注释读取 xsi:type
问题描述
我想将基于 jaxb 的 xml 文件读入我的面向对象结构。
可以说这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<children xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<child xsi:type="girl">
<age>12</age>
<isdancing>true</isdancing>
</child>
<child xsi:type="boy">
<age>10</age>
<issoccerplayer>true</issoccerplayer>
</child>
</children>
children是某种包装元素,包括多个子元素。孩子可以是由 xsi:type 指定的男孩或女孩。这两个类有一些共同的元素(如age)和一些不同的(排除)元素(如isdancing或issoccerplayer)
要读取文件,我有这个方法:
public static void main( String[] args ) throws JAXBException
{
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(Children.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
File file = new File("C:/test.xml");
if (!file.exists()) System.out.println("File does not exist");
Children children = (Children) jaxbUnmarshaller.unmarshal(file);
System.out.println(children.toString());
}
我的儿童课程如下所示:
@XmlRootElement(name="children")
@XmlAccessorType(XmlAccessType.FIELD)
public class Children {
@XmlElement(name="child")
private List<Child> childrenList;
public List<Child> getChildren() { return childrenList; }
public void setChildren(List<Child> children) {this.childrenList = children;}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
我的孩子类看起来像这样:
@XmlAccessorType(XmlAccessType.FIELD)
public class Child {
@XmlAttribute(name="xsi:type")
private XsiType xsiType;
private int age;
@XmlElement(name = "isdancing")
private boolean isDancing;
@XmlElement(name = "issoccerplayer")
private boolean isSoccerPlayer;
//Getter and setter for all fields
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
我的 XsiType 类看起来像这样:
@XmlAccessorType(XmlAccessType.FIELD)
public class XsiType {
@XmlAttribute(name="xsi:type")
private String name;
@XmlValue
private String value;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getValue() { return value;
public void setValue(String value) { this.value = value; }
}
在我的 pom.xml 中,我包含了以下依赖项:
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
我现在的问题是,输出没问题,但是 Child-class 的元素 xsiType 始终为 null,否则最终会出现与 XmlTest.model.Child.xsiType 相关的 IllegalAnnotationExceptions
所以我希望设置任何类型的@Xml-Annotation 都会出错。有人可以通过找出错误来帮助我吗?
目标是迭代子列表并在运行时(基于 xsiType)决定这是女孩还是男孩。
谢谢
解决方案
你不需要你的XsiType
课。你可以String
改用。
在您的Child
班级中,该xsiType
属性应如下所示。
@XmlAttribute(name = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance")
private String xsiType;
注意:在@XmlAttribute
注释中
- 使用
name = "type"
(不带前缀xsi:
) - 指定
namespace
XML 中给定的参数xmlns:xsi="..."
顺便说一句:
最好使用常量
,而不是键入字符串。所以你改进的代码会是这样的:"http://www.w3.org/2001/XMLSchema-instance"
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI
@XmlAttribute(name = "type", namespace = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
private String xsiType;
推荐阅读
- algorithm - groupBy + count 有启发式算法吗?
- python - 点击时tkinter显示框架
- oracle - 触发 + 变异错误 + 级联更新
- oracle - 在多个 pl sql 块上使用条件
- c++ - 事件 QComboBox 到自定义 QLineEdit
- django - Django 将迁移问题更改为新的 Postgres DB
- mysql - mysql - 如何在 JOIN 查询后在结果表中显示连接表的主键?
- alexa - Alexa无需“询问”技能即可调用自定义意图
- sql - 查找 column1 与 column2 匹配的行(可能在另一行中)
- php - php类常量或类构造错误?