java jaxb einfache Analyse ist erforderlich @XmlAccessorType(XmlAccessType.BEREICH) Anmerkung

Ich versuche zu analysieren, eine xml zu java-Objekten, habe ich gelesen und umgesetzt, das folgende tutorial:

http://www.vogella.com/articles/JAXB/article.html (funktioniert perfekt)

Aber wenn ich meine eigenen Klassen (ähnlich wie in dem tutorial)

Bekomme ich: Exception in thread "main" com.Sonne.xml.intern.binden.v2.- Laufzeit.IllegalAnnotationsException: 1 Grafen von IllegalAnnotationExceptions
- Klasse zwei Eigenschaften den gleichen Namen "clienteList"

Es sei denn, ich benutze @XmlAccessorType(XmlAccessType.FELD) auf Klasse Clientes aber im tutorial nicht verwendet wird.

Irgendwelche Ideen ?

(Es funktioniert gut mit der @XmlAccessorType(XmlAccessType.BEREICH) Anmerkung aber möchte ich wissen, warum ist es erforderlich, dass mit meinen Klassen, während es ist nicht für die Klassen im tutorial)

Vielen Dank im Voraus für jede information.

Klasse Cliente

package mx.com.findep.crediseguros.dto.servicios.finsol;

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

@XmlRootElement(name = "cliente")
public class Cliente {

  private String numeroPersona;

  @XmlElement(name = "numeroPersona")
  public String getNumeroPersona() {
    return numeroPersona;
  }

  public void setNumeroPersona(String numeroPersona) {
    this.numeroPersona = numeroPersona;
  }

} 

Klasse Clientes

package mx.com.findep.crediseguros.dto.servicios.finsol;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "clientes")
//@XmlAccessorType(XmlAccessType.FIELD) //without this line it fails
public class Clientes {

      //XmLElementWrapper generates a wrapper element around XML representation
      @XmlElementWrapper(name = "clienteList")
      //XmlElement sets the name of the entities
      @XmlElement(name = "cliente")
      private ArrayList<Cliente> clienteList;


      public void setClienteList(ArrayList<Cliente> clienteList) {
        this.clienteList = clienteList;
      }

      public ArrayList<Cliente> getClienteList() {
        return clienteList;
      }


    }

Meine Prüfung Marshalling

package mx.com.findep.crediseguros.dto.servicios.finsol;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


public class TestClientesXml {

  private static final String SOME_XML = "C:/bea/user_projects/domains/DominioDesarrollo/esquemas/calculoCostoSeguroPeticion.xml";

  public static void main(String[] args) throws JAXBException, IOException {

    ArrayList<Cliente> clienteList = new ArrayList<Cliente>();

    Cliente cliente1 = new Cliente();
    cliente1.setNumeroPersona("1");

    clienteList.add(cliente1);

    Cliente cliente2 = new Cliente();
    cliente2.setNumeroPersona("2");
    clienteList.add(cliente2);

    Clientes clientes = new Clientes();

    clientes.setClienteList(clienteList);

    JAXBContext context = JAXBContext.newInstance(Clientes.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    m.marshal(clientes, System.out);

    m.marshal(clientes, new File(SOME_XML));

    System.out.println();
    System.out.println("Output from our XML File: ");
    Unmarshaller um = context.createUnmarshaller();
    Clientes clientes2 = (Clientes) um.unmarshal(new FileReader(SOME_XML));
    ArrayList<Cliente> list = clientes2.getClienteList();
    for (Cliente cliente : list) {
      System.out.println("Cliente: " + cliente.getNumeroPersona());
    }
  }
} 

InformationsquelleAutor Jonathan Caballero | 2013-11-06

Schreibe einen Kommentar