当前位置: 首页 > 知识库问答 >
问题:

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

童浩言
2023-03-14

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

http://www.vogella.com/articles/JAXB/article.html(效果很好)

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

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

除非我使用@XmlAccessorType(XmlAccessType.字段)在类客户端,但在教程中不被使用。

有什么想法吗?

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

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

共有2个答案

黄向明
2023-03-14

假设我们有:

@XmlRootElement(name = "book")
public class Book {
    @XmlElement(name = "book_title")
    private String title;
    public getTitle(){..}
    public setTitle(){...}
    }

如果我们运行代码,线程“main”中将出现异常

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "title"
    this problem is related to the following location:
        at public java.lang.String com.example.jaxb.Book.getTitle()
        at com.example.jaxb.Book
    this problem is related to the following location:
        at private java.lang.String com.example.jaxb.Book.title
        at com.example.jaxb.Book

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

@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {

错误将消失。

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

莫宝
2023-03-14

默认情况下,JAXB将公共字段和属性视为映射。如果您注释一个字段,它会将该字段和属性视为导致冲突的映射。没有@XmlAccessorType(XmlAccessType.字段)您应该注释get或set方法。

了解更多信息

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
 类似资料:
  • 可能的重复: Java是什么?:运算符叫什么,它做什么? 你好,我在理解下面的代码时遇到了一些问题,有人能帮我弄到吗? 我不明白是什么意思。

  • 我正在为web服务编写一个Java客户端,需要将响应JSON对象解析为

  • 我有一个json文件,大约1MB。我正在尝试用Klaxon库解析这个JSON,但需要大约30秒。我不想使用SQLite。我能做什么?

  • 使用iphoneSDK官方NSXMLParserDelegate做的简单xml解析,附带详细注释以及使用到的方法的详细解释,非常适合新手参考。 [Code4App.com]

  • 问题内容: 根据Java Concurrency in Practice,第11.4.3章说: 锁拆分有时可以扩展为对一组可变对象的独立对象进行分区锁,在这种情况下,这称为锁拆分。例如,ConcurrentHashMap的实现使用了一个由16个锁组成的数组,每个锁保护着1/16的哈希桶。桶N由锁N mod 16保护。 我仍然无法理解和可视化锁条和桶机制。有人可以用很好的理解力来解释这个问题吗:)

  • 问题内容: 我正在尝试解析WSDL以获取操作,端点和示例有效负载。用户输入的WSDL。我找不到执行此操作的教程。 我只能找到那些生成不需要的源代码的代码。我尝试使用XBeans,但显然我需要Saxon。没有撒克逊人,有没有简单的轻巧的方法来做到这一点? 例如 应该进行操作:GetLastTradePrice,GetLastTradePrice 端点:StockQuotePort 有效负载样本: 这