我正在尝试将xml解析为java对象,我已经阅读并实现了以下教程:

http://www.vogella.com/articles/JAXB/article.html(完美运行)

但是当我创建自己的小节时(类似于本教程中的小节)

我得到:线程“main”中的异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1个IllegalAnnotationExceptions计数
类具有名称相同的两个属性“clienteList”

除非我在Clientes类上使用@XmlAccessorType(XmlAccessType.FIELD),但在本教程中没有使用。

有任何想法吗 ?

(它可以与@XmlAccessorType(XmlAccessType.FIELD)批注一起很好地工作,但是我想知道为什么我的 class 需要它,而本教程中的类却不需要它)

预先感谢您提供任何信息。

类客户

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

}

类客户
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;
      }


    }

测试我的编组
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());
    }
  }
}

最佳答案

默认情况下,JAXB将公共字段和属性视为已映射。如果对字段进行注释,则它将认为该字段和属性已映射,从而导致冲突。如果没有@XmlAccessorType(XmlAccessType.FIELD),则应注释get或set方法。

有关更多信息

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
  • 10-07 17:00