java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)批注 [英] java jaxb simple parsing is requiring @XmlAccessorType(XmlAccessType.FIELD) annotation

查看:180
本文介绍了java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to parse an xml to java objects, I've read and implemented the following tutorial:

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

http://www.vogella.com/articles/JAXB/article.html (works perfectly)

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

But when I create my own clases (similar to those in the tutorial)

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

I get: Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Class has two properties of the same name "clienteList"

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

Unless I use @XmlAccessorType(XmlAccessType.FIELD) on class Clientes but in the tutorial is not being used.

有什么想法吗?

(它与@XmlAccessorType(XmlAccessType.FIELD)批注配合正常,但我想知道为什么我的班级要求使用它,而本教程中的班级却不需要它)

(It works fine with the @XmlAccessorType(XmlAccessType.FIELD) annotation but I want to know why is it being required with my classes while it is not for the classes in the tutorial)

在此先感谢您提供任何信息.

Thank you in advance for any information.

Class Cliente

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

} 

类客户

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方法.

By default JAXB treats public fields and properties as mapped. If you annotate a field it then considers the field and property as mapped causing the conflict. Without @XmlAccessorType(XmlAccessType.FIELD) you should annotate the get or set method.

更多信息

这篇关于java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆