JAXB/Jackson:没有parent标签的两个元素的序列
JAXB/Jackson: Sequence of two elements without parent tag
更新:寻找 Jackson 或 JAXB 解决方案。
在对 Jackson 的行为进行了一些研究之后,我发现 Jackson 总是使用 collections 的包装器。所以我需要的可能与杰克逊无关。因此,将 JAXB 添加到标题中。
原题
我需要为以下 XML 模式创建 POJO。
<ABWrap>
<A></A>
<B></B>
<A></A>
<B></B>
...
... n times
</ABWrap>
我试过关注 POJO。但是这些并没有产生预期的结果。
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 的回答:
我定义了ABWrap
、ABInterface
和A
如下:
@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 到 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();
}
}
更新:寻找 Jackson 或 JAXB 解决方案。
在对 Jackson 的行为进行了一些研究之后,我发现 Jackson 总是使用 collections 的包装器。所以我需要的可能与杰克逊无关。因此,将 JAXB 添加到标题中。
原题
我需要为以下 XML 模式创建 POJO。
<ABWrap>
<A></A>
<B></B>
<A></A>
<B></B>
...
... n times
</ABWrap>
我试过关注 POJO。但是这些并没有产生预期的结果。
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 的回答:
我定义了ABWrap
、ABInterface
和A
如下:
@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 到 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();
}
}