Jaxb/Jackson:没有父级标签的两个元素的序列



update :寻找杰克逊 jaxb解决方案。

在研究了杰克逊的行为后,我发现杰克逊将始终将包装纸用于收藏。因此,我可能不可能与杰克逊(Jackson(一起做。因此,将jaxb添加到标题中。


原始问题

我需要创建pojo以遵循XML模式。

<ABWrap>
    <A></A>
    <B></B>
    <A></A>
    <B></B>
    ...
    ... n times
</ABWrap>

我尝试关注波约斯。但是这些不是产生所需的结果。

class AB {
    @JacksonXmlProperty(localName = "A")
    private String A;
    @JacksonXmlProperty(localName = "B")
    private String B;
}
@JacksonXmlRootElement(localName = "ABWrap")
class ABWrap {
    @JacksonXmlElementWrapper(useWrapping = false)
    private AB[] ab = new AB[n];
}

我需要维持一对<A></A><B></B>的条件。元素的顺序很重要。
在我的情况下,以下模式将不起作用:

<ABWrap>
    <A></A>
    <A></A>
    ...
    ... n times
    <B></B>
    <B></B>
    ...
    ... n times
</ABWrap>

我能够取得第二个。但是我无法找到一种生成第一个模式的方法。


@mart的答案上更新

我定义了ABWrapABInterfaceA如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ABWrap")
public class ABWrap {
    @XmlElements({@XmlElement(name = "A", type = A.class), @XmlElement(name = "B", type = B.class)})
    private List<ABInterface> ab;
}
public interface ABInterface { }
public class A implements ABInterface {
    @XmlValue
    private String a;
}

B定义类似于A

主要方法如下:

public class Application {
    public static void main(final String[] args) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ABWrap.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        A a = new A("a");
        B b = new B("b");
        ABWrap abWrap = new ABWrap(Arrays.asList(a, b));
        marshaller.marshal(abWrap, System.out);
    }
}

但是此解决方案失败了以下错误:(jaxbpoc是项目名称(

If a class has @XmlElement property, it cannot have @XmlValue property.
this problem is related to the following location:
    at private java.lang.String ...jaxbpoc.A.a
    at ...jaxbpoc.A
    at private java.util.List ...jaxbpoc.ABWrap.ab
    at ...jaxbpoc.ABWrap
this problem is related to the following location:
    at public java.lang.String ...A.getA()
    at ...jaxbpoc.A
    at private java.util.List ...jaxbpoc.ABWrap.ab
    at ...jaxbpoc.ABWrap
If a class has @XmlElement property, it cannot have @XmlValue property.
this problem is related to the following location:
    at private java.lang.String ...jaxbpoc.B.b
    at ...jaxbpoc.B
    at private java.util.List ...jaxbpoc.ABWrap.ab
    at ...jaxbpoc.ABWrap
    ....
    ....
Class has two properties of the same name "a"
this problem is related to the following location:
    at public java.lang.String ...jaxbpoc.A.getA()
    at ...jaxbpoc.A
    at private java.util.List ...jaxbpoc.ABWrap.ab
    at ...jaxbpoc.ABWrap
this problem is related to the following location:
    ....
    ....

您可以做到这一点:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ABWrap")
public class ABWrap {
    @XmlElements({
            @XmlElement(name="A", type = A.class),
            @XmlElement(name="B", type = B.class),
    })
    private List<Letter> letters;
}

和a,b看起来像这样:

public class A implements Letter {
    @XmlValue
    private String a;
}

和A,B的常见接口不做太多:

public interface Letter { }

更新:

正如我在评论中提到的那样,我尝试了XML对Pojo,反之亦然,这起作用了。我在这里粘贴了我用来测试的简单程序,所以请让我知道它如何为您服务,因此我可以进一步探索。

xmltopojo:

public static void main(String[] args) {
        try {
            File file = new File("AB.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(ABWrap.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            ABWrap pojo = (ABWrap) jaxbUnmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

和pojo to xml:

public static void main(String[] args) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(ABWrap.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            A a = new A("testA");
            B b = new B("testB");
            ABWrap abWrap = new ABWrap(Arrays.asList(a, b));
            marshaller.marshal(abWrap, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

最新更新