java 骆驼路线:将 xml 读入 pojo 并将其写回 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30122184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:31:20  来源:igfitidea点击:

camel route: read xml into pojo and write it back into xml file

javaxmlspringapache-camel

提问by David

I was looging for some time now, but since a lot of configuration files are in xml it's hard to find some answers to my question.

我已经找了一段时间了,但是由于很多配置文件都在 xml 中,因此很难找到我的问题的答案。

What would I like to do? Using a caml route I want to read in an xml file and put it into a POJO. Here I want to analyze it. At the end I want to write a different xml file (POJO) as an answer into an out folder.

我想做什么?使用caml路由我想读入一个xml文件并将其放入POJO中。这里我想分析一下。最后,我想将不同的 xml 文件 (POJO) 作为答案写入 out 文件夹。

My problem is, that I don't know how to tell camel to parse the xml file body into my POJO.

我的问题是,我不知道如何告诉骆驼将 xml 文件体解析到我的 POJO 中。

A short example what I did until know:

我在知道之前所做的一个简短示例:

My camel route:

我的骆驼路线:

from("file:data/in")
                    .marshal().xstream()
                    .bean(XmlToBeanAndBackBean.class)
                    .unmarshal().xstream()
                    .to("file:data/out");

My POJO:

我的POJO:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlFilePojo {

     @XmlAnyAttribute
     private String name;
     @XmlElement(name = "the_age")
     private int theAge;

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

And my Bean which is used in the camel route:

还有我在骆驼路线中使用的 Bean:

@Component
public class XmlToBeanAndBackBean {

    public XmlFilePojo transformXmlObject(XmlFilePojo xmlFilePojo){
        XmlFilePojo returnPojo = xmlFilePojo;
        returnPojo.setName("merkur");
        return returnPojo;
    }
}

I think that my error is in the camel route which camel trys to converting the xml file into the XmlFilePojo Object.

我认为我的错误出在骆驼尝试将 xml 文件转换为 XmlFilePojo 对象的骆驼路线中。

When I try to run it I get the following error:

当我尝试运行它时,出现以下错误:

Caused by: org.apache.camel.InvalidPayloadException: No body available of type: XmlFilePojo but has value: [B@659392cd of type: byte[] on: simple.xml. Caused by: No type converter available to convert from type: byte[] to the required type: XmlFilePojo with value [B@659392cd. Exchange[simple.xml]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: byte[] to the required type: XmlFilePojo with value [B@659392cd]

引起:org.apache.camel.InvalidPayloadException:没有可用的主体类型:XmlFilePojo 但具有值:[B@659392cd 类型:byte[] on:simple.xml。原因:没有类型转换器可用于从类型:byte[] 转换为所需的类型:XmlFilePojo,值为 [B@659392cd。交换[simple.xml]。引起:[org.apache.camel.NoTypeConversionAvailableException - 没有类型转换器可用于从类型:byte[] 转换为所需的类型:XmlFilePojo with value [B@659392cd]

Since I don't have a byte[] in my file I don't know how to handle this. Hope someone has an answer.

由于我的文件中没有 byte[],我不知道如何处理。希望有人有答案。

回答by Claus Ibsen

Just add camel-jaxbto the classpath and it can do the automatic xml <--> pojo conversition, when you are using JAXB annotations on your POJOs. Just write your bean code using the POJOs.

camel-jaxb当您在 POJO 上使用 JAXB 注释时,只需添加到类路径,它就可以自动进行 xml <--> pojo 转换。只需使用 POJO 编写您的 bean 代码。

Then the route is simple

然后路线很简单

from("file:data/in")
    .bean(XmlToBeanAndBackBean.class)
    .to("file:data/out");

回答by Victor Tripeno

You can use unmarshal with json library.

您可以将 unmarshal 与 json 库一起使用。

from("file:data/in")
.unmarshal().json(JsonLibrary.Hymanson, XmlFilePojo.class)
.to("file:data/out");