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

时间:2013-11-06 20:15:14

标签: java xml xml-parsing jaxb jaxb2

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

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

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

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

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

有什么想法吗?

(它适用于@XmlAccessorType(XmlAccessType.FIELD)注释,但我想知道为什么我的类需要它而不是教程中的类)

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

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

} 

Class Clientes

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 个答案:

答案 0 :(得分:5)

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

了解更多信息

答案 1 :(得分:0)

所以我们说:

void resourse(const TickType_t xFrequency)
{
    TickType_t xLastWakeTime;
    xLastWakeTime = xTaskGetTickCount();
    while(1)
    {
    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
    vTaskDelayUntil( &xLastWakeTime, xFrequency);
    }
}
xSemaphoreHandle botton_one = 0;
void botton(void* r)
{

    while(1)
       {
        if(xSemaphoreTake(botton_one, 1))
        {
        HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
        xSemaphoreGive(botton_one);
        }
        vTaskDelay(1);

       }
}
void normal_blinking(void* r)
{
    while(1)
       {
        if(xSemaphoreTake(botton_one, 1))
        {
            resourse(500);
            xSemaphoreGive(botton_one);
        }

      }
}
void fast_blinking(void* s)
{
    while(1){
        if((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin))== 0)
        {
        xSemaphoreTake(botton_one, 1);
        resourse(50);
        xSemaphoreGive(botton_one);
        }
        vTaskDelay(2000);
        vTaskSuspend(NULL);
    }
}

int main(void)
 {
    TaskHandle_t xHandle;
    botton_one = xSemaphoreCreateMutex();
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

     xTaskCreate(botton, (const char*)"task_1", 1024, 0, 3, 0);
     xTaskCreate(normal_blinking, (const char*)"task_2", 1024, 0, 2,0);
     xTaskCreate(fast_blinking, (const char*)"task_3", 1024, 0, 1,0);

     vTaskStartScheduler();

     while (1){
  }
}

如果我们运行我们将拥有的代码 线程&#34; main&#34;中的例外情况

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

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

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

错误将消失。

当有想要编组并且有10个字段的类时,我更喜欢仅注释字段,而不是一次设置setter然后是getter。因此,请使用@XmlAccessorType并仅注释字段。