【问题标题】:How to unmarshal XML file in apache camel如何在 Apache Camel 中解组 XML 文件
【发布时间】:2021-08-25 19:13:35
【问题描述】:

我有两个类 ItemsDto 和 ItemDto 它们代表 XML 文件中的数据

@XmlR

ootElement(name = "items")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemsDto {

    @XmlElementRef
    private List<ItemDto> items;

    public List<ItemDto> getItems() {
        return items;
    }

    public void setItems(List<ItemDto> items) {
        this.items = items;
    }
}

还有一个

@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDto {

    @XmlElement
    private int itemNo;
    @XmlElement
    private String name;
    @XmlElement
    private String description;
    @XmlElement
    private int number;
    @XmlElement
    private int rating;

    public ItemDto() {
    }

    public ItemDto(int itemNo, String name, String description, int number) {
        this.itemNo = itemNo;
        this.name = name;
        this.description = description;
        this.number = number;
    }

    //getters and setters

}

我有 test.xml 文件

<items>
  <item>
    <itemNo>998</itemNo>
    <name>Rider Auto Part Generic Brakepad</name>
    <description>Basic brakepad for any motorbike</description>
    <number>100</number>
  </item>
</items>

我想设置从文件读取的路径,但我不知道如何设置解组部分 我不知道该怎么办...

from("file:inputXML")
.unmarshal().  something
.process(exchange -> {
   System.out.println(exchange.getIn().getBody());
  })

【问题讨论】:

    标签: xml routes apache-camel unmarshalling


    【解决方案1】:
        unmarshal(new JaxbDataFormat(JAXBContext.newInstance(ItemsDto.class))).process(e-> exchange.getIn().getBody(ItemsDto.class);)
    

    之后你可以在处理器中使用这个类

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多