我想知道如何使用JAXB
添加xml元素像这样的现状
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ml>
<button x="102" y="100" width="187" height="123" id="null1" className="Button1" text="Button1"/>
</ml>
主要问题是,当我只添加元素时,会添加不必要的根元素(例如:\ tag ml \ tag / ml)。(<button>
)。
所以,上面的例子正在执行两次
除了根元素之外,我只需要添加元素。
因此,我想要的一个例子就像下面的
所以,如果你能给我示例来源,请评论!!!
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ml>
<button x="1" y="2" width="3" height="4" id="string" className="string" title="string"/>
<attribute x="1" y="2" width="3" height="4" id="string" className="string" title="string"/>
</ml>
答案 0 :(得分:0)
如果您不想使用XSD:ANY元素,那么有一个示例如何通过XSD定义多个元素:CHOICE。您正在寻找的抽象元素可能是JAXBElement,这意味着任何类型为T的元素。
<强> XSD:强>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.wicketstuff.cz/p/bossflow/examples/test"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns:test="http://www.wicketstuff.cz/p/bossflow/examples/test">
<xsd:annotation>
<xsd:documentation>Test version 0.1</xsd:documentation>
</xsd:annotation>
<xsd:attributeGroup name="coordinates">
<xsd:attribute name="x" use="optional" type="xsd:integer"></xsd:attribute>
<xsd:attribute name="y" use="optional" type="xsd:integer"></xsd:attribute>
</xsd:attributeGroup>
<xsd:attributeGroup name="size">
<xsd:attribute name="width" use="optional" type="xsd:integer"></xsd:attribute>
<xsd:attribute name="height" use="optional" type="xsd:integer"></xsd:attribute>
</xsd:attributeGroup>
<xsd:attributeGroup name="attrs">
<xsd:attribute name="id" use="required" type="xsd:string"></xsd:attribute>
<xsd:attribute name="className" use="optional" type="xsd:string"></xsd:attribute>
<xsd:attribute name="text" use="optional" type="xsd:string"></xsd:attribute>
</xsd:attributeGroup>
<xsd:complexType name="Element">
<xsd:annotation>
<xsd:documentation>Abstract ancestor of all elements</xsd:documentation>
</xsd:annotation>
<xsd:attributeGroup ref="test:attrs"></xsd:attributeGroup>
<xsd:attributeGroup ref="test:coordinates"></xsd:attributeGroup>
<xsd:attributeGroup ref="test:size"></xsd:attributeGroup>
</xsd:complexType>
<xsd:complexType name="Ml">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="button" type="test:Element"></xsd:element>
<xsd:element name="attribute" type="test:Element"></xsd:element>
</xsd:choice>
</xsd:complexType>
<xsd:element name="ml" type="test:Ml"></xsd:element>
</xsd:schema>
JAXB生成的Java Ml.class:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
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.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Ml", propOrder = {
"buttonOrAttribute"
})
public class Ml {
@XmlElementRefs({
@XmlElementRef(name = "attribute", namespace = "http://www.wicketstuff.cz/p/bossflow/examples/test", type = JAXBElement.class),
@XmlElementRef(name = "button", namespace = "http://www.wicketstuff.cz/p/bossflow/examples/test", type = JAXBElement.class)
})
protected List<JAXBElement<Element>> buttonOrAttribute;
public List<JAXBElement<Element>> getButtonOrAttribute() {
if (buttonOrAttribute == null) {
buttonOrAttribute = new ArrayList<JAXBElement<Element>>();
}
return this.buttonOrAttribute;
}
}
JAXB生成的Java Element.class:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Element")
public class Element {
@XmlAttribute(name = "x")
protected Integer x;
@XmlAttribute(name = "y")
protected Integer y;
@XmlAttribute(name = "width")
protected Integer width;
@XmlAttribute(name = "height")
protected Integer height;
@XmlAttribute(name = "id", required = true)
protected String id;
@XmlAttribute(name = "className")
protected String className;
@XmlAttribute(name = "text")
protected String text;
public Integer getX() {
return x;
}
public void setX(Integer value) {
this.x = value;
}
public Integer getY() {
return y;
}
public void setY(Integer value) {
this.y = value;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer value) {
this.width = value;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer value) {
this.height = value;
}
public String getId() {
return id;
}
public void setId(String value) {
this.id = value;
}
public String getClassName() {
return className;
}
public void setClassName(String value) {
this.className = value;
}
public String getText() {
return text;
}
public void setText(String value) {
this.text = value;
}
}