gpt4 book ai didi

jaxb - 如何使用错误的父/子模型解码 xml 消息

转载 作者:行者123 更新时间:2023-12-04 13:34:44 26 4
gpt4 key购买 nike

我正在尝试将第 3 方 XML 有效负载解码到一个类中。问题是有效载荷具有父/子关系,并且根节点、父节点和子节点都具有相同的元素名称。这是有效负载的示例。

<?xml version="1.0" encoding="UTF-8"?>
<Directory>
<id>2</id>
<name>Media</name>
<Directory>
<id>5</id>
<name>Default_Content</name>
<Directory>
<id>9</id>
<name>Images</name>
</Directory>
<Directory>
<id>8</id>
<name>Icons</name>
</Directory>
<Directory>
<id>6</id>
<name>Additional_Content</name>
</Directory>
</Directory>
<Directory>
<id>12</id>
<name>IC</name>
</Directory>
</Directory>

所以我试图注释一个类,以便 JAXB/JAX-RS 可以将其解码为有用的东西。

我试过这样的东西

@XmlRootElement(name="Directory")
public class Directory {
private int id;
private String name;

@XmlElement(name="Directory");
private List<Directory> directories = new ArrayList<Directory>();
}

但是,可以预见的是,它会抛出 IllegalAnnotationException因为有两个同名的属性。

关于如何使用 JAXB/JAX-RS 干净地处理这种困惑的任何想法,还是我应该自己解析它?

最佳答案

简短回答

异常(exception)是由于字段/属性冲突。您可以注释属性(获取方法)或在您的类型上设置以下注释:

@XmlAccessorType(XmlAccessType.FIELD)
public class Directory {
...
}

长答案

JAXB 的默认访问类型是 PUBLIC_MEMBER这意味着 JAXB 将映射所有公共(public)字段(实例变量)和属性(get/set 方法)。

public class Foo {

private String bar;

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

}

如果您注释一个字段:

public class Foo {

@XmlAttribute
private String bar;

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

}

那么 JAXB 会认为它有两个 bar属性映射并引发异常:
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "bar"
this problem is related to the following location:
at public java.lang.String example.Foo.getBar()
at example.Foo
this problem is related to the following location:
at private java.lang.String example.Foo.bar
at example.Foo

解决方法是对属性进行注解,并将 XmlAccessType 类型设置为 FIELD
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

@XmlAttribute
private String bar;

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

}

您的型号

目录

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Directory")
@XmlAccessorType(XmlAccessType.FIELD)
public class Directory {
private int id;
private String name;

@XmlElement(name="Directory")
private List<Directory> directories = new ArrayList<Directory>();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Directory> getDirectories() {
return directories;
}

public void setDirectories(List<Directory> directories) {
this.directories = directories;
}

}

演示

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Directory.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
Directory directory = (Directory) unmarshaller.unmarshal(new File("input.xml"));

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(directory, System.out);
}

}

关于jaxb - 如何使用错误的父/子模型解码 xml 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6062882/

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