0

maybe my question are similiar like these question
How do I parse this XML in Java with JAXB?
i want to generate some XML files to display some data but i have different problem with using some @XmlAttribute & @XmlValue properly in JAXB
this is XML output supposed to be:

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

since i'm using JAXB 2 days ago i dont know how to use it properly, and its kinda hard for me to read the JAXB documentation guide because my native language isn't English. this is my java code for JAXB

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

this is the error i get

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
RedHatSource
  • 9
  • 1
  • 7

2 Answers2

0
  1. This is XML, not XML schema
  2. you have some errors in your XML file: tag item is not closed on some lines. After fixing this you can create XSD from XML on one of online services, for example: https://www.freeformatter.com/xsd-generator.html
  3. With your XSD file you can create JAXB binding classes with xjc utility (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html) from JDK

upd the generated class:

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

            }

        }

    }

}
Nikita
  • 94
  • 1
  • 3
  • Sorry about my question, the things i want to know is how to write XML nested tags with some attributes using JAXB. – RedHatSource May 05 '19 at 12:39
0

i found some solution but it still didnt solve the real problem that i facing now. i found this tutorial HERE
and i change little bit of my code

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

placing variable in the right order & remove @XmlElement

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

removed @XmlValue and re-arrange @XmlAttribute to bottom line code

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

Material.java

i did same to material, remove @XmlValue and its variable
    @XmlAttribute
    public String getTipe() {
        return tipe;
    }
    public void setTipe(String tipe) {
        this.tipe = tipe;
    }

and the rest i remove ana.setValue(1); from Demo.java
and this are the result i get for my XML files.

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

its almost look like what i wanted but still there is some child element that i wanted to change

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

to this

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

should i create additional class for item? or tweak some code in Material.java ?

RedHatSource
  • 9
  • 1
  • 7