简体   繁体   中英

how can i add xml element using JAXB

i want to know about adding xml elements using JAXB

present state like this

<?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>

Main problem is that unnecessary root element(ex : \\tag ml \\tag /ml) add when I add only element.( <button> ).

so, above example is executing twice

I need to add element only except root element.

consequently, a shape of example I want is like below

so if you can give me example source, comment please!!!

<?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>

If you do not want to use XSD:ANY element, so there is an example how to define more than one element by XSD:CHOICE. The abstract element you are looking for is probably JAXBElement that means any element of type 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>

Java Ml.class generated by JAXB:

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;
    }

}

Java Element.class generated by JAXB:

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM