如果一个class有@XmlElement属性,它不能有@XmlValue属性错误
If a class has @XmlElement property, it cannot have @XmlValue property error
当我尝试编组低于 java classes 低于预期 xml.
时,我遇到异常
异常:
如果 class 有 @XmlElement 属性,它不能有 @XmlValue 属性.
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>
Parent.java
@XmlRootElement(name = "parent")
public class Parent{
protected List<Child> child= new ArrayList<Child>();
protected List<String> text= new ArrayList<String>();
@XmlElementRef(name="child",type=Child.class)
public List<Child> getChild() {
return child;
}
@XmlMixed
public List<String> getText() {
return text;
}
public void setChild(Child value) {
this.child.add(value);
}
public void setText(String value) {
this.text.add(value);
}
}
Child.java
public class Child {
@XmlValue
protected String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
对于上面的 XML 我尝试了这个并且对我来说似乎工作正常:
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>
Parent.class:
@XmlRootElement(name = "parent")
@Data
@NoArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {
@XmlMixed
@XmlAnyElement
private List<Object> textContent;
@XmlElement(name = "child")
private String child;
}
我的主class:
public class JaxbExampleMain {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("parent.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Parent.class).createUnmarshaller();
final Parent parent = unmarshaller.unmarshal(xmlStreamReader, Parent.class).getValue();
System.out.println(parent.toString());
Marshaller marshaller = JAXBContext.newInstance(Parent.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(parent, System.out);
}
}
以下是我得到的输出:
Parent(textContent=[
I am the parent element of mixedtype.
,
], child=I am the child element)
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>
当我尝试编组低于 java classes 低于预期 xml.
时,我遇到异常异常: 如果 class 有 @XmlElement 属性,它不能有 @XmlValue 属性.
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>
Parent.java
@XmlRootElement(name = "parent")
public class Parent{
protected List<Child> child= new ArrayList<Child>();
protected List<String> text= new ArrayList<String>();
@XmlElementRef(name="child",type=Child.class)
public List<Child> getChild() {
return child;
}
@XmlMixed
public List<String> getText() {
return text;
}
public void setChild(Child value) {
this.child.add(value);
}
public void setText(String value) {
this.text.add(value);
}
}
Child.java
public class Child {
@XmlValue
protected String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
对于上面的 XML 我尝试了这个并且对我来说似乎工作正常:
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>
Parent.class:
@XmlRootElement(name = "parent")
@Data
@NoArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {
@XmlMixed
@XmlAnyElement
private List<Object> textContent;
@XmlElement(name = "child")
private String child;
}
我的主class:
public class JaxbExampleMain {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("parent.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Parent.class).createUnmarshaller();
final Parent parent = unmarshaller.unmarshal(xmlStreamReader, Parent.class).getValue();
System.out.println(parent.toString());
Marshaller marshaller = JAXBContext.newInstance(Parent.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(parent, System.out);
}
}
以下是我得到的输出:
Parent(textContent=[
I am the parent element of mixedtype.
,
], child=I am the child element)
<parent>
I am the parent element of mixedtype.
<child>I am the child element</child>
</parent>