如何在JAVA中使用JAXB正确生成XML

时间:2019-05-05 08:08:42

标签: java xml annotations jaxb

也许我的问题与这些问题类似
How do I parse this XML in Java with JAXB?
我想生成一些XML文件以显示一些数据 但是我在JAXB中正确使用一些 @XmlAttribute @XmlValue 有不同的问题,这是 XML 输出应该是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnlKerja>
    <Analisa no="1">
        <Kode>B.1</Kode>
        <Uraian>some description</Uraian>
        <Material>
            <item type="value">ID|ID|ID</item>
        </Material>
        <Jumlah>some value</Jumlah>
    </Analisa>
    <Analisa no="2">
        <Kode>B.3</Kode>
        <Uraian>some description</Uraian>
        <Material>
            <item type="value">ID|ID|ID</item>
            <item type="value">ID|ID|ID</item>
        </Material>
        <Jumlah>some value</Jumlah>
    </Analisa>
</AnlKerja>

因为2天前我正在使用JAXB 我不知道如何正确使用它, 这对我来说有点难以阅读JAXB文档指南 因为我的母语不是英语。 这是我的 JAXB

的Java代码

AnlKerja.java

@XmlRootElement(name = "AnlKerja")
public class AnlKerja {
    @XmlElementWrapper(name = "Analisa")
    @XmlElement(name = "Analisa")
    private ArrayList<Analisa> analisa;

    public ArrayList<Analisa> getAnl() {
        return analisa;
    }

    public void setAnl(ArrayList<Analisa> anl) {
        this.analisa = anl;
    }
}

Analisa.java

@XmlRootElement(name="Analisa")
@XmlType(propOrder = {"no","value","kode","uraian","material","jumlah"})
public class Analisa {


    @XmlAttribute
    private String no;
    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    @XmlValue
    private int value;
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    private String kode,uraian;
    private double jumlah;

    @XmlElement(name="Kode")
    public String getKode() {
        return kode;
    }

    public void setKode(String kode) {
        this.kode = kode;
    }
     @XmlElement(name="Uraian")
    public String getUraian() {
        return uraian;
    }

    public void setUraian(String uraian) {
        this.uraian = uraian;
    }
    @XmlElementWrapper(name = "material")
    private ArrayList<Material> material;
    public ArrayList<Material> getMaterial() {
        return material;
    }

    public void setMaterial(ArrayList<Material> material) {
        this.material = material;
    }
     @XmlElement(name="Jumlah")
    public double getJumlah() {
        return jumlah;
    }

    public void setJumlah(double jumlah) {
        this.jumlah = jumlah;
    }
}

Material.java

@XmlRootElement(name = "Material")
public class Material {
    @XmlElement(name = "items")
    private String items;

    public Material() {
        this.items = "";
        this.tipe = "";
        this.value = "";
    }
    public Material(String[] isi, String tipe, String deskripsi) {
        int temp = isi.length;
        for (int x=0; x<temp; x++) {
            this.items += isi[x]+"|";
        };
        this.value = deskripsi;
        this.tipe = tipe;
    }
    public String getItems() {
        return items;
    }

    public void setItems(String items) {
        this.items = items;
    }
    public void setItems(String[] items) {
        int temp = items.length;
        for (int x=0; x<temp; x++) {
            this.items += items[x]+"|";
        }
    }

    @XmlAttribute
    private String tipe;
    public String getTipe() {
        return tipe;
    }

    public void setTipe(String tipe) {
        this.tipe = tipe;
    }

    @XmlValue
    private String value;
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Demo.java

public class Demo {
    private static final String STORE_XML = "results/ANL.xml";

    public static void main(String[] args) throws JAXBException, IOException{
        ArrayList<Material> mtr = new ArrayList<Material>();
        ArrayList<Analisa> anl = new ArrayList<Analisa>();
        AnlKerja anKerja = new AnlKerja();

        Material mat = new Material();
        mat.setTipe("tipe");
        mat.setValue("Bahan");
        mat.setItems("MT001|MT002|MT003");
        mtr.add(mat);
        mat.setTipe("tipe");
        mat.setValue("Tenaga");
        mat.setItems("MT0001|MT0002|MT0003");
        mtr.add(mat);

        Analisa ana = new Analisa();
        ana.setNo("no");
        ana.setValue(1);
        ana.setKode("B.1");
        ana.setUraian("some description");
        ana.setMaterial(mtr);
        ana.setJumlah(122414.03);
        anl.add(ana);

        anKerja.setAnl(anl);

        JAXBContext jCont = JAXBContext.newInstance(AnlKerja.class);
        Marshaller marshal = jCont.createMarshaller();
        marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshal.marshal(anKerja, System.out);

        marshal.marshal(anKerja, new File(STORE_XML));
    }
}

这是我得到的错误

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
If a class has @XmlElement property, it cannot have @XmlValue property.
    this problem is related to the following location:
        at private int XML.Analisa.value
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at public int XML.Analisa.getValue()
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
If a class has @XmlElement property, it cannot have @XmlValue property.
    this problem is related to the following location:
        at private java.lang.String XML.Material.value
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at public java.lang.String XML.Material.getValue()
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "material"
    this problem is related to the following location:
        at public java.util.ArrayList XML.Analisa.getMaterial()
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "no"
    this problem is related to the following location:
        at public java.lang.String XML.Analisa.getNo()
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private java.lang.String XML.Analisa.no
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "value"
    this problem is related to the following location:
        at public int XML.Analisa.getValue()
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private int XML.Analisa.value
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "items"
    this problem is related to the following location:
        at public java.lang.String XML.Material.getItems()
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private java.lang.String XML.Material.items
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "tipe"
    this problem is related to the following location:
        at public java.lang.String XML.Material.getTipe()
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private java.lang.String XML.Material.tipe
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
Class has two properties of the same name "value"
    this problem is related to the following location:
        at public java.lang.String XML.Material.getValue()
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja
    this problem is related to the following location:
        at private java.lang.String XML.Material.value
        at XML.Material
        at private java.util.ArrayList XML.Analisa.material
        at XML.Analisa
        at private java.util.ArrayList XML.AnlKerja.analisa
        at XML.AnlKerja

2 个答案:

答案 0 :(得分:0)

  1. 这是XML,而不是XML模式
  2. 您的XML文件中有一些错误:标记项在某些行上未关闭。 解决此问题后,您可以在一种在线服务上从XML创建XSD,例如:https://www.freeformatter.com/xsd-generator.html
  3. 使用XSD文件,您可以使用JDK的xjc实用程序(https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html)创建JAXB绑定类

更新生成的类:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2019.05.05 at 08:59:07 PM MSK 
//


package generated;

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.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Analisa" maxOccurs="unbounded" minOccurs="0">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                   &lt;element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                   &lt;element name="Material">
 *                     &lt;complexType>
 *                       &lt;complexContent>
 *                         &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                           &lt;sequence>
 *                             &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
 *                               &lt;complexType>
 *                                 &lt;simpleContent>
 *                                   &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
 *                                     &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
 *                                   &lt;/extension>
 *                                 &lt;/simpleContent>
 *                               &lt;/complexType>
 *                             &lt;/element>
 *                           &lt;/sequence>
 *                         &lt;/restriction>
 *                       &lt;/complexContent>
 *                     &lt;/complexType>
 *                   &lt;/element>
 *                   &lt;element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                 &lt;/sequence>
 *                 &lt;attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "analisa"
})
@XmlRootElement(name = "AnlKerja")
public class AnlKerja {

    @XmlElement(name = "Analisa")
    protected List<AnlKerja.Analisa> analisa;

    /**
     * Gets the value of the analisa property.
     * 
     * <p>
     * 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 <CODE>set</CODE> method for the analisa property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getAnalisa().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link AnlKerja.Analisa }
     * 
     * 
     */
    public List<AnlKerja.Analisa> getAnalisa() {
        if (analisa == null) {
            analisa = new ArrayList<AnlKerja.Analisa>();
        }
        return this.analisa;
    }


    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="Material">
     *           &lt;complexType>
     *             &lt;complexContent>
     *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *                 &lt;sequence>
     *                   &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
     *                     &lt;complexType>
     *                       &lt;simpleContent>
     *                         &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
     *                           &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
     *                         &lt;/extension>
     *                       &lt;/simpleContent>
     *                     &lt;/complexType>
     *                   &lt;/element>
     *                 &lt;/sequence>
     *               &lt;/restriction>
     *             &lt;/complexContent>
     *           &lt;/complexType>
     *         &lt;/element>
     *         &lt;element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *       &lt;/sequence>
     *       &lt;attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "kode",
        "uraian",
        "material",
        "jumlah"
    })
    public static class Analisa {

        @XmlElement(name = "Kode", required = true)
        protected String kode;
        @XmlElement(name = "Uraian", required = true)
        protected String uraian;
        @XmlElement(name = "Material", required = true)
        protected AnlKerja.Analisa.Material material;
        @XmlElement(name = "Jumlah", required = true)
        protected String jumlah;
        @XmlAttribute(name = "no")
        protected Byte no;

        /**
         * Gets the value of the kode property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getKode() {
            return kode;
        }

        /**
         * Sets the value of the kode property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setKode(String value) {
            this.kode = value;
        }

        /**
         * Gets the value of the uraian property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getUraian() {
            return uraian;
        }

        /**
         * Sets the value of the uraian property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setUraian(String value) {
            this.uraian = value;
        }

        /**
         * Gets the value of the material property.
         * 
         * @return
         *     possible object is
         *     {@link AnlKerja.Analisa.Material }
         *     
         */
        public AnlKerja.Analisa.Material getMaterial() {
            return material;
        }

        /**
         * Sets the value of the material property.
         * 
         * @param value
         *     allowed object is
         *     {@link AnlKerja.Analisa.Material }
         *     
         */
        public void setMaterial(AnlKerja.Analisa.Material value) {
            this.material = value;
        }

        /**
         * Gets the value of the jumlah property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getJumlah() {
            return jumlah;
        }

        /**
         * Sets the value of the jumlah property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setJumlah(String value) {
            this.jumlah = value;
        }

        /**
         * Gets the value of the no property.
         * 
         * @return
         *     possible object is
         *     {@link Byte }
         *     
         */
        public Byte getNo() {
            return no;
        }

        /**
         * Sets the value of the no property.
         * 
         * @param value
         *     allowed object is
         *     {@link Byte }
         *     
         */
        public void setNo(Byte value) {
            this.no = value;
        }


        /**
         * <p>Java class for anonymous complex type.
         * 
         * <p>The following schema fragment specifies the expected content contained within this class.
         * 
         * <pre>
         * &lt;complexType>
         *   &lt;complexContent>
         *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
         *       &lt;sequence>
         *         &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
         *           &lt;complexType>
         *             &lt;simpleContent>
         *               &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
         *                 &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
         *               &lt;/extension>
         *             &lt;/simpleContent>
         *           &lt;/complexType>
         *         &lt;/element>
         *       &lt;/sequence>
         *     &lt;/restriction>
         *   &lt;/complexContent>
         * &lt;/complexType>
         * </pre>
         * 
         * 
         */
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "item"
        })
        public static class Material {

            protected List<AnlKerja.Analisa.Material.Item> item;

            /**
             * Gets the value of the item property.
             * 
             * <p>
             * 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 <CODE>set</CODE> method for the item property.
             * 
             * <p>
             * For example, to add a new item, do as follows:
             * <pre>
             *    getItem().add(newItem);
             * </pre>
             * 
             * 
             * <p>
             * Objects of the following type(s) are allowed in the list
             * {@link AnlKerja.Analisa.Material.Item }
             * 
             * 
             */
            public List<AnlKerja.Analisa.Material.Item> getItem() {
                if (item == null) {
                    item = new ArrayList<AnlKerja.Analisa.Material.Item>();
                }
                return this.item;
            }


            /**
             * <p>Java class for anonymous complex type.
             * 
             * <p>The following schema fragment specifies the expected content contained within this class.
             * 
             * <pre>
             * &lt;complexType>
             *   &lt;simpleContent>
             *     &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
             *       &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
             *     &lt;/extension>
             *   &lt;/simpleContent>
             * &lt;/complexType>
             * </pre>
             * 
             * 
             */
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "value"
            })
            public static class Item {

                @XmlValue
                protected String value;
                @XmlAttribute(name = "type")
                protected String type;

                /**
                 * Gets the value of the value property.
                 * 
                 * @return
                 *     possible object is
                 *     {@link String }
                 *     
                 */
                public String getValue() {
                    return value;
                }

                /**
                 * Sets the value of the value property.
                 * 
                 * @param value
                 *     allowed object is
                 *     {@link String }
                 *     
                 */
                public void setValue(String value) {
                    this.value = value;
                }

                /**
                 * Gets the value of the type property.
                 * 
                 * @return
                 *     possible object is
                 *     {@link String }
                 *     
                 */
                public String getType() {
                    return type;
                }

                /**
                 * Sets the value of the type property.
                 * 
                 * @param value
                 *     allowed object is
                 *     {@link String }
                 *     
                 */
                public void setType(String value) {
                    this.type = value;
                }

            }

        }

    }

}

答案 1 :(得分:0)

我找到了一些解决方案,但仍然没有解决我现在面临的真正问题。 我找到了本教程HERE
然后我更改了一些代码

AnlKerja.java

@XmlRootElement
public class AnlKerja {
    private ArrayList<Analisa> analisa;
    // removed @XmlElementWrapper & @XmlElement
    public ArrayList<Analisa> getAnalisa() {
        return analisa;
    }
    public void setAnalisa(ArrayList<Analisa> anl) {
        this.analisa = anl;
    }
}

Analisa.java

以正确的顺序放置变量并删除@XmlElement

    private String kode,uraian;
    private ArrayList<Material> material;
    private double jumlah;
    private String no;

已删除@XmlValue 并将@XmlAttribute重新排列为底行代码

    @XmlAttribute
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }

Material.java

我对材料也做同样的操作,删除@XmlValue及其变量

    @XmlAttribute
    public String getTipe() {
        return tipe;
    }
    public void setTipe(String tipe) {
        this.tipe = tipe;
    }

其余的我从 Demo.java
中删除了ana.setValue(1); 这就是我获取XML文件的结果。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<anlKerja>
    <analisa no="1">
        <kode>B.1</kode>
        <uraian>Some Description</uraian>
        <material tipe="Tenaga">
            <items>MT001|MT002|MT003</items>
        </material>
        <material tipe="Bahan">
            <items>MT0001|MT0002|MT0003</items>
        </material>
        <jumlah>122414.03</jumlah>
    </analisa>
</anlKerja>

它几乎看起来像我想要的,但是仍然有一些我想要更改的子元素

<material tipe="Bahan">
    <items>MT0001|MT0002|MT0003</items>
</material>

对此

<Material>
    <item type="Bahan">ID|ID|ID</item>
</Material>
Or this
<Material>
    <item type="Bahan">ID|ID|ID</item>
    <item type="Tenaga">ID|ID|ID</item>
</Material>

我应该为商品创建其他类别吗? 或在 Material.java 中调整一些代码?