使用相同标签名称但属性不同将XML映射到对象

时间:2019-06-05 19:17:43

标签: java xml jaxb moxy

我正在尝试使用JAXB和Eclipse Persistence oxm注释包将下面的XML字符串转换为Java对象。

<output>
     <rtEvent>
           <eventData name="tcppayload">
               <data>111111-000000-111111</data>
           </eventData>
           <eventData name="text">
               <data>ABCD</data>
           </eventData>
     </rtEvent>
</output>

但是,反序列化似乎不起作用。有人可以指出我可能做错了吗。

下面是我用来将字符串反序列化为对象的类

@XmlRootElement(name = "output")
@XmlAccessorType(XmlAccessType.FIELD)
public class CameraTriggerOutput {

       @XmlPath("/rtEvent/eventData[@name=tcppayload]/data/text()")
       private String data;

 public void toXml() {
    try {
        JAXBContext ctx = JAXBContext.newInstance(CameraTriggerOutput.class);
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(this, System.out);
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
  }
 }

运行后,我得到以下输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><output/>

1 个答案:

答案 0 :(得分:1)

我在代码下方提供了纯Jaxb,您可以尝试。

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "eventData")
public class EventData {

  private String name;

  private String data;

  @XmlElement(name = "data")
  public String getData() {
    return data;
  }

  public void setData(String data) {
    this.data = data;
  }

  @XmlAttribute(name = "name")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "rtEvent")
public class RtEvent {

  private List<EventData> edataList;

  @XmlElement(name = "eventData")
  public List<EventData> getEdataList() {
    return edataList;
  }

  public void setEdataList(List<EventData> edataList) {
    this.edataList = edataList;
  }
}

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "output")
public class Output {

  private RtEvent rtEvent;

  public RtEvent getRtEvent() {
    return rtEvent;
  }

  public void setRtEvent(RtEvent rtEvent) {
    this.rtEvent = rtEvent;
  }

  public static void main(String[] args) {
    try {
      EventData eData1 = new EventData();
      eData1.setData("111111-000000-111111");
      eData1.setName("tcppayload");

      EventData eData2 = new EventData();
      eData2.setData("ABCD");
      eData2.setName("text");

      List<EventData> eDataList = new ArrayList<>();
      eDataList.add(eData1);
      eDataList.add(eData2);

      RtEvent rtEvent = new RtEvent();
      rtEvent.setEdataList(eDataList);

      Output output = new Output();
      output.setRtEvent(rtEvent);

      JAXBContext ctx = JAXBContext.newInstance(Output.class);
      Marshaller marshaller = ctx.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      marshaller.marshal(output, System.out);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

您只是Jaxb,因为它是Java的一部分,因此不需要包含任何其他注释。您可以在自己的IDE中创建单个类,并且可以测试具有主要方法的Output类。