Error creating XML file with java

0

Hello everyone, I have the following program:

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
 *
 * @author Montse
 */
@XmlRootElement
@XmlType(propOrder ={"Autor", "Nom", "Editorial","Isbn", "llibre"})

public class Llibreria extends MarshalClass{
    static String JAXB_FORMATTED_OUTPUT;
    private String Autor;
    private String Nom;
    private String Editorial;
    private int Isbn;
    private ArrayList<LlistaLlibre> llibre;


    public String getAutor(){return Autor;}
    @XmlElement
    public void setAutor (String Autor){this.Autor =Autor;}

    public String getNom(){return Nom;}
    @XmlElement
    public void setNom (String Nom){this.Nom =Nom;}

    public String getEditorial(){return Editorial;}
    @XmlElement
    public void setEditorial (String Editorial){this.Editorial =Editorial;}

    public int getIsbn(){return Isbn;}
    @XmlElement
    public void setIsbn (int Isbn){this.Isbn =Isbn;}

    public ArrayList <LlistaLlibre> getLlibre() { return llibre;}
    @XmlElement
    public void setLlibre(ArrayList<LlistaLlibre> llibre){ this.llibre= llibre;}

}



package exercici3;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Montse
 */
@XmlRootElement(name="llibr")

class LlistaLlibre extends MarshalClass {

    private String autor;
    private String nom;
    private String editorial;
    private int isbn;

    public String getAutor(){ return autor;}
    @XmlElement
    public void setAutor (String autor){ this.autor = autor;}

    public String getNom(){ return nom;}
    @XmlElement
    public void setNom (String nom){ this.nom = nom;}

    public String getEditorial(){ return editorial;}
    @XmlElement
    public void setEditorial (String editorial){ this.editorial = editorial;}

     public int getIsbn(){ return isbn;}
    @XmlElement
    public void setIsbn (int isbn){ this.isbn = isbn;}


    }




package exercici3;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Montse
 */
public class MarshalClass {

    public void generateXML (String nameFile) {

        try{
            File file = new File (nameFile);
            JAXBContext jc = JAXBContext.newInstance(this.getClass());
            Marshaller jaxbMarshaller = jc.createMarshaller();

            jaxbMarshaller.setProperty(Llibreria.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(this, new FileWriter(nameFile, true));

        }catch (JAXBException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}


package exercici3;

import java.util.ArrayList;

/**
 *
 * @author Montse
 */
public class LlibreriaEscriure {

    public static void main(String[]args){

        EscriureLlibreria();

    }

    private static void EscriureLlibreria() {

        Llibreria cc = new Llibreria();
        cc.setAutor("Xavier");
        cc.setNom("Nosotros dos");
        cc.setEditorial("Columna");
        cc.setIsbn(978-84-664-2228-4);

        ArrayList<LlistaLlibre> alCU = new ArrayList<LlistaLlibre>();
        int init = 2000;
                for (int i =1; i<1000;i++){
        LlistaLlibre cu = new LlistaLlibre();
        cu.setAutor(""+i);
        cu.setNom(""+i);
        cu.setEditorial(""+i);
        cu.setIsbn(i);
        alCU.add(cu);       
    }
                cc.setLlibre(alCU);
                cc.generateXML("llibres.xml");


    }

}

I get the following error:

run:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
La propiedad autor existe, pero no está especificada en @XmlType.propOrder
    this problem is related to the following location:
        at public java.lang.String exercici3.Llibreria.getAutor()
        at exercici3.Llibreria
La propiedad editorial existe, pero no está especificada en @XmlType.propOrder
    this problem is related to the following location:
        at public java.lang.String exercici3.Llibreria.getEditorial()
        at exercici3.Llibreria
La propiedad isbn existe, pero no está especificada en @XmlType.propOrder
    this problem is related to the following location:
        at public int exercici3.Llibreria.getIsbn()
        at exercici3.Llibreria
La propiedad nom existe, pero no está especificada en @XmlType.propOrder
    this problem is related to the following location:
        at public java.lang.String exercici3.Llibreria.getNom()
        at exercici3.Llibreria
La propiedad Autor aparece en @XmlType.propOrder, pero no existe. Quizá quiso decir autor.
    this problem is related to the following location:
        at exercici3.Llibreria
La propiedad Nom aparece en @XmlType.propOrder, pero no existe. Quizá quiso decir nom.
    this problem is related to the following location:
        at exercici3.Llibreria
La propiedad Editorial aparece en @XmlType.propOrder, pero no existe. Quizá quiso decir editorial.
    this problem is related to the following location:
        at exercici3.Llibreria
La propiedad Isbn aparece en @XmlType.propOrder, pero no existe. Quizá quiso decir isbn.
    this problem is related to the following location:
        at exercici3.Llibreria

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at exercici3.MarshalClass.generateXML(MarshalClass.java:25)
    at exercici3.LlibreriaEscriure.EscriureLlibreria(LlibreriaEscriure.java:41)
    at exercici3.LlibreriaEscriure.main(LlibreriaEscriure.java:18)

Can you help me?

EDITO

I have made modifications to the code but now I have this other error:

Exception in thread "main" java.lang.IllegalArgumentException: name parameter must not be null
    at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:328)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
    at exercici3.MarshalClass.generateXML(MarshalClass.java:28)
    at exercici3.LlibreriaEscriure.EscriureLlibreria(LlibreriaEscriure.java:37)
    at exercici3.LlibreriaEscriure.main(LlibreriaEscriure.java:14)
C:\Users\Montse\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
    
asked by Montse Mkd 27.09.2017 в 20:55
source

1 answer

1

I think it's due to bad practices in your code. For example:

public class Llibreria extends MarshalClass{
    static String JAXB_FORMATTED_OUTPUT;
    private String Autor;
    private String Nom;
    private String Editorial;
    private int Isbn;
    private ArrayList<LlistaLlibre> llibre;

All variables start with a capital letter except for free and if you realize it is the only one that does not appear in the error. In java the variable names must start in lowercase and the classes in uppercase, so that the naming convention works correctly with the getters and setters (The getters and setters are well named in your code). This convention is strong that frameworks like hibernate use it a lot.

private String autor;
@XmlElement
public String getAutor(){return autor;}
public void setAutor (String autor){this.autor =autor;}

And another thing that I observe is that the annotation @XmlElement must go in the getters and not in the setters as you are putting it, since it indicates that from there the value of an xml tag will be obtained.

These changes imply that the @XmlType change to @XmlType(propOrder ={"autor", "nom", "editorial","isbn", "llibre"})

    
answered by 27.09.2017 / 21:36
source