gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:04 25 4
gpt4 key购买 nike

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

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

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

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

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

有什么想法吗?

(它与 @XmlAccessorType(XmlAccessType.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;

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 将公共(public)字段和属性视为已映射。如果您注释一个字段,它会认为该字段和属性已映射,从而导致冲突。如果没有 @XmlAccessorType(XmlAccessType.FIELD),您应该注释 get 或 set 方法。

了解更多信息

关于java jaxb简单解析需要@XmlAccessorType(XmlAccessType.FIELD)注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com