java—使用javax.xml绑定用点定义xml元素

6l7fqoea  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(199)

我需要创建一个与此结构匹配的xml:

<OfferPackage Name="aaa">
    <OfferPackage.Offers>
        <OfferCollection Capacity="1">
            <Offer Price="12.34"/>
            <Offer Price="12.34"/>
        </OfferCollection>
    </OfferPackage.Offers>
</OfferPackage>

我的问题是我不知道如何做到这一点: <OfferPackage.Offers> . 我怎样才能用注解得到这个“点符号”?我出来的都是这样的:

<OfferPackage Name="aaa">
  <OfferCollection Capacity="1">
    <Offer Price="12.34"/>
    <Offer Price="12.34"/>
  </OfferCollection>
</OfferPackage>

这是我当前的代码:

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

    @XmlAttribute(name = "Name")
    private String name = null;

    @XmlElement( name = "OfferCollection")
    private List<OfferCollection> offers = null;

    @SneakyThrows
    public String toString() {
        var sw = new StringWriter();
        var marshaller = JAXBContext
                .newInstance(OfferPackage.class)
                .createMarshaller();

        marshaller.setProperty(JAXB_FRAGMENT, true);
        marshaller.marshal(this, sw);

        return sw.toString();
    }
}
@XmlRootElement(name = "Offer")
@XmlAccessorType(XmlAccessType.FIELD)
public class OfferCollection {

    @XmlAttribute(name = "Capacity")
    private Integer capacity = null;

    @XmlElement(name = "Offer")
    private List<Offer> offer = null;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题