繁体   English   中英

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

[英]How to generate XML properly with JAXB in JAVA

也许我的问题与这些问题类似
如何使用JAXB在Java中解析此XML?
properly in JAXB 我想生成一些XML文件来显示一些数据,但我在JAXB中正确使用有不同的问题
这是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
  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;
                }

            }

        }

    }

}

我找到了一些解决方案,但它仍然没有解决我现在面临的真正问题。 我在这里找到了这个教程
我改变了一点代码

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及其变量

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

其余的我删除ana.setValue(1); 来自Demo.java
这是我得到的XML文件的结果。

<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> <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中调整一些代码?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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