Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)注释_Java_Xml_Xml Parsing_Jaxb_Jaxb2 - Fatal编程技术网

java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)注释

java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)注释,java,xml,xml-parsing,jaxb,jaxb2,Java,Xml,Xml Parsing,Jaxb,Jaxb2,我正在尝试将xml解析为java对象,我已经阅读并实现了以下教程: (工作完美) 但是当我创建自己的类时(类似于教程中的类) 我在线程“main”com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException中获得:异常:1个illegalannotationException计数 类有两个同名的属性“ClientList” 除非我在类客户机上使用@XmlAccessorType(XmlAccessType.FIELD),但在教程

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

(工作完美)

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

我在线程“main”com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException中获得:异常:1个illegalannotationException计数 类有两个同名的属性“ClientList”

除非我在类客户机上使用@XmlAccessorType(XmlAccessType.FIELD),但在教程中没有使用

有什么想法吗

(它与@xmlacessortype(xmlacesstype.FIELD)注释配合使用效果很好,但我想知道为什么我的类需要它,而教程中的类却不需要它)

提前感谢您提供的任何信息

阶级客户

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;
导入java.util.ArrayList;
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlElementWrapper;
导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name=“客户”)
//@XmlAccessorType(XmlAccessType.FIELD)//如果没有此行,它将失败
公共类客户{
//XmLElementWrapper围绕XML表示生成一个包装器元素
@XmlElementWrapper(name=“客户列表”)
//XmlElement设置实体的名称
@XmlElement(name=“客户”)
私人ArrayList客户名单;
public void setclientlist(ArrayList clientlist){
this.clientlist=客户列表;
}
公共阵列列表GetClientList(){
返回客户名单;
}
}
测试我的编组

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());
    }
  }
} 
package mx.com.findep.crediseguros.dto.servicios.finsol;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
公共类TestClientsXML{
私有静态最终字符串SOME_XML=“C:/bea/user_projects/domains/dominiaodesarrollo/esquemas/calculoCostoSeguroPeticion.XML”;
公共静态void main(字符串[]args)抛出jaxbeexception、IOException{
ArrayList客户端列表=新建ArrayList();
Cliente client1=新客户();
客户1.setNumeroPersona(“1”);
客户列表。添加(客户1);
Cliente client2=新客户();
客户2.setNumeroPersona(“2”);
客户列表。添加(客户2);
客户客户=新客户();
clientes.setclientlist(clientlist);
JAXBContext context=JAXBContext.newInstance(Clientes.class);
Marshaller m=context.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式的_输出,Boolean.TRUE);
m、 马歇尔(客户,系统输出);
m、 封送(客户,新文件(一些XML));
System.out.println();
System.out.println(“来自XML文件的输出:”);
Unmarshaller um=context.createUnmarshaller();
clientesclientes2=(Clientes)um.unmarshal(新文件阅读器(一些XML));
ArrayList=clients2.getClientList();
针对(客户:列表){
System.out.println(“Cliente:+Cliente.getNumeroPersona());
}
}
} 

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

了解更多信息

所以我们可以说:

@XmlRootElement(name = "book")
public class Book {
    @XmlElement(name = "book_title")
    private String title;
    public getTitle(){..}
    public setTitle(){...}
    }
如果我们运行代码,我们将 线程“main”中出现异常

但是如果我们添加注释:XmlAccessorType

@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
错误将消失


当有一个类需要marshall并且有10个字段时,我更喜欢只注释字段,而不是一次注释setter,然后注释getter。因此,请使用@XmlAccessorType并仅对字段进行注释。

但是在教程的示例中(链接的示例)与我的代码完全相同,并且它没有注释,不是吗?@code4jhon-这是因为在示例中,list属性有一个getter,但没有setter。这意味着默认情况下JAXB不会将其视为映射。这让我抓狂,非常感谢,只是它有一个setter,但没有getter。再次感谢。:)有没有一种没有注释的方法可以做到这一点?
@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {