1

大家好,我想代表以下 xml 代码:

        <AggregationMeta>
            <AggregationLink>
                <LocalName>stuff</LocalName>
                <Value>stuff</Value>
                <Meta/>
            </AggregationLink>
            <AggregationLink>
                <LocalName>stuff</LocalName>
                <Value>stuff</Value>
                <Meta/>
            </AggregationLink>
        </AggregationMeta>

我想使用@XmlElementWrapper 注释来做到这一点。

这是我使用它的类:

public class VrIdnMeta {

private String lri;
private String nodeLocalName;
private List<Link> referenceLink;
private List<Link> aggregationLink;
private List<SimpleLink> backLink;
private List<SimpleLink> incomingChangeLink;

...

@XmlElementWrapper(name = "ReferenceMeta")
@XmlElement(name = "ReferenceLink")
public List<Link> getReferenceLink() {
    return referenceLink;
}

public void setReferenceLink(List<Link> referenceLink) {
    this.referenceLink = referenceLink;
}

@XmlElementWrapper(name = "AggregationMeta")
@XmlElement(name = "AggregationLink")
public List<Link> getAggregationLink() {
    return aggregationLink;
}

public void setAggregatioinLink(List<Link> aggregationLink) {
    this.aggregationLink = aggregationLink;
}

@XmlElementWrapper(name = "BackLinkMeta")
@XmlElement(name = "BackLink")
public List<SimpleLink> getBackLink() {
    return backLink;
}

public void setBackLink(List<SimpleLink> backLink) {
    this.backLink = backLink;
}

@XmlElementWrapper(name = "IncomingChangeMeta")
@XmlElement(name = "IncomingChangeLink")
public List<SimpleLink> getIncomingChangeLink() {
    return incomingChangeLink;
}

public void setIncomingChangeLink(List<SimpleLink> incomingChangeLink) {
    this.incomingChangeLink = incomingChangeLink;
}

}

这是 SimpleLink 类

public class SimpleLink {

private String localName;
private String value;

...

@XmlElement(name = "LocalName")
public String getLocalName() {
    return localName;
}

public void setLocalName(String localName) {
    this.localName = localName;
}

@XmlElement(name = "Value")
public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

最后是 Link 类

public class Link extends SimpleLink{

private String meta;

public Link(String localName, String value, String meta) {
    super(localName, value);
    this.meta = meta;
}

public Link() {
    super();
}

public Link(String localName, String value) {
    super(localName, value);
}

@XmlElement(name = "Meta")
public String getMeta() {
    return meta;
}

public void setMeta(String meta) {
    this.meta = meta;
}

}

当我使用 Jaxb2 解组与我上面写的类似的 xml 时,问题就出现了:我得到一个 NullPointerException :-(。我读到 jaxb 中有一个关于这个事实的错误,但它在 2006 年得到了解决,我正在使用 spring 3(用他自己的jaxb2库)。

有人有线索吗?

4

1 回答 1

1

NPE 似乎源于您的 get 和 set 方法的名称不匹配(在 中VrIdnMeta):

@XmlElementWrapper(name = "AggregationMeta")
@XmlElement(name = "AggregationLink")
public List<Link> getAggregationLink() {
    return aggregationLink;
}

public void setAggregatioinLink(List<Link> aggregationLink) {
    this.aggregationLink = aggregationLink;
}

当我更改为时setAggregatioinLinksetAggregationLink我让您的示例按预期运行。

于 2011-05-13T20:26:57.623 回答