gpt4 book ai didi

java - JAXB 将嵌套元素解码为空

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:20 24 4
gpt4 key购买 nike

XML 片段:

<datasource formatted-name="blah" inline="blah">
<repository-location derived-from="blah" id="blah" path="blah" revision="blah" site="blah"/>
</datasource>

我正在尝试使用嵌套静态类解码一个类 (DataSource) 下的所有内容。这是我的数据源类:

@XmlRootElement(name = "datasource")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSource {

@XmlAttribute(name = "formatted-name")
protected String formattedName;
@XmlAttribute(name = "inline")
protected String inline;
@XmlElement(name = "repository-location")
protected RepositoryLocation repositoryLocation;

// public getters and setters for fields above

@XmlAccessorType(XmlAccessType.FIELD)
public static class RepositoryLocation {

@XmlAttribute(name = "derived-from")
protected String derivedFrom;
@XmlAttribute(name = "id")
protected String id;
@XmlAttribute(name = "path")
protected String path;
@XmlAttribute(name = "revision")
protected String revision;
@XmlAttribute(name = "site")
protected String site;

// public getters and setters for fields above
}
}

解码器:

JAXBContext jaxbContext = JAXBContext.newInstance(DataSource.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(responseXML);
dataSourceResponse = (DataSource) unmarshaller.unmarshal(reader);

我可以成功输出数据源字段“formattedName”和“inline”,但“repositoryLocation”为空。有人可以帮忙吗?

最佳答案

JAXB 能够在没有 Getters/Setters 的情况下解码,字段甚至可以是私有(private)的。给定上面的 DataSource 类,将一些生成的 toString 方法添加到 DataSourceRepositoryLocation 中,以下打印出来所有属性:

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import java.io.StringReader;

public class Jaxb {

public static void main(String[] args) throws JAXBException {
String xml = "<datasource formatted-name=\"blah\" inline=\"blah\">\n" +
" <repository-location derived-from=\"blah\" id=\"blah\"" +
" path=\"blah\" revision=\"blah\" site=\"blah\"/>\n" +
"</datasource>";

JAXBContext context = JAXBContext.newInstance(DataSource.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
DataSource dataSource = (DataSource) unmarshaller.unmarshal(new StringReader(xml));
System.out.println(dataSource);
}

@XmlRootElement(name = "datasource")
@XmlAccessorType(XmlAccessType.FIELD)
private class DataSource {

@XmlAttribute(name = "formatted-name")
private String formattedName;
@XmlAttribute(name = "inline")
private String inline;
@XmlElement(name = "repository-location")
private RepositoryLocation repositoryLocation;

@XmlAccessorType(XmlAccessType.FIELD)
private class RepositoryLocation {

@XmlAttribute(name = "derived-from")
private String derivedFrom;
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "path")
private String path;
@XmlAttribute(name = "revision")
private String revision;
@XmlAttribute(name = "site")
private String site;

@Override
public String toString() {
return "RepositoryLocation{" +
"derivedFrom='" + derivedFrom + '\'' +
", id='" + id + '\'' +
", path='" + path + '\'' +
", revision='" + revision + '\'' +
", site='" + site + '\'' +
'}';
}
}

@Override
public String toString() {
return "DataSource{" +
"formattedName='" + formattedName + '\'' +
", inline='" + inline + '\'' +
", repositoryLocation=" + repositoryLocation +
'}';
}
}
}

关于java - JAXB 将嵌套元素解码为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780183/

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