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

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

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

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

我得到:线程中的异常"main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:IllegalAnnotationExceptions的1个计数 Class has two properties of the same name "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;

}

}

class 客户

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

public void setClienteList(ArrayList clienteList) {

this.clienteList = clienteList;

}

public ArrayList 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 clienteList = new ArrayList();

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 list = clientes2.getClienteList();

for (Cliente cliente : list) {

System.out.println("Cliente: " + cliente.getNumeroPersona());

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值