gpt4 book ai didi

java - 如何读取xsi :type with java annotations

转载 作者:行者123 更新时间:2023-11-30 07:40:05 28 4
gpt4 key购买 nike

我想将基于 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 是某种包装元素,包括多个 child 元素。 child 可以是男孩或女孩,由 xsi:type 指定。这两个类有一些共同的元素(如 age)和一些不同的(不包括)元素(如 isdancingissoccerplayer)

要读取文件,我有这个方法:

    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());
}

我的 Children 类如下所示:

    @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 都会出错。有人可以帮我找出错误吗?

目标是迭代 child 列表并在运行时(基于 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;

关于java - 如何读取xsi :type with java annotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com