My output in java: jaxbContext = JAXBContext.newInstance(Div.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); jaxbUnmarshaller.unmarshal(file); System.out.println("id " + div1.getSpan().get(0).get(id) + "value " + div1.getSpan().get(0).get(id)) // should print: id = x1x1 value = bla bla
我有下一堂课:
public class Span List
div; public List
getDiv() { return div; } @XmlElement public void setDiv(List
div) { for (int i = 0 ; i<div.size(); i++){ System.out.print("element")} this.div = div; }
和:
public class Div List span = div1.get @XmlElement public void setSpan(List span) { for (int i = 0 ; i<span.size(); i++){ System.out.print("element")} this.span = span; } public List
现在,我也想要跨度的值(“bla bla”)。 所以我添加到Span类:
String value; public String getValue() { return value; } @XmlValue public void setValue(String value) { this.value = value; }
它给了我下一个错误:
If a class has '@XmlElement' property, it cannot have '@XmlValue' property.
@XmlRootElement public class Div { List items = new ArrayList(); @XmlElementRef(name="span", type=Span.class) @XmlMixed public List getItems() { return items; } public void setItems(List items) { this.items = items; } }
演示
public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Span.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Span span = (Span) unmarshaller.unmarshal(new StringReader("Text
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.03.22 at 01:15:22 PM MST // package example; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlElementRefs; import javax.xml.bind.annotation.XmlMixed; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** *
Java class for anonymous complex type. * *
The following schema fragment specifies the expected content contained within this class. * *
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "content" }) @XmlRootElement(name = "div") public class Div { @XmlElementRefs({ @XmlElementRef(name = "div", type = Div.class), @XmlElementRef(name = "span", type = Span.class) }) @XmlMixed protected Listcontent; /** * Gets the value of the content property. * *
* This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a set method for the content property. * *
* For example, to add a new item, do as follows: *
* getContent().add(newItem); *
* * *
* Objects of the following type(s) are allowed in the list * {@link Div } * {@link String } * {@link Span } * * */ public ListgetContent() { if (content == null) { content = new ArrayList(); } return this.content; } }
例如/ Span.java
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.03.22 at 01:15:22 PM MST // package example; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlMixed; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** *
Java class for anonymous complex type. * *
The following schema fragment specifies the expected content contained within this class. * *
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "content" }) @XmlRootElement(name = "span") public class Span { @XmlElementRef(name = "div", type = Div.class) @XmlMixed protected Listcontent; @XmlAttribute @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NCName") protected String id; /** * Gets the value of the content property. * *
* This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a set method for the content property. * *
* For example, to add a new item, do as follows: *
* getContent().add(newItem); *
* * *
* Objects of the following type(s) are allowed in the list * {@link Div } * {@link String } * * */ public ListgetContent() { if (content == null) { content = new ArrayList(); } return this.content; } /** * Gets the value of the id property. * * @return * possible object is * {@link String } * */ public String getId() { return id; } /** * Sets the value of the id property. * * @param value * allowed object is * {@link String } * */ public void setId(String value) { this.id = value; } }
例如/ ObjectFactory.java
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See http://java.sun.com/xml/jaxb // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.03.22 at 01:15:22 PM MST // package example; import javax.xml.bind.annotation.XmlRegistry; /** * This object contains factory methods for each * Java content interface and Java element interface * generated in the example package. *
An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class. * */ @XmlRegistry public class ObjectFactory { /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: example * */ public ObjectFactory() { } /** * Create an instance of {@link Div } * */ public Div createDiv() { return new Div(); } /** * Create an instance of {@link Span } * */ public Span createSpan() { return new Span(); } }