如何使用camel-xstream定义自定义名称空间和标签别名 [英] How to define custom namespace and tag alias using camel-xstream

查看:371
本文介绍了如何使用camel-xstream定义自定义名称空间和标签别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将管道定界字符串转换为xml,这很好用.但是,我没有得到下面显示的实际输出.在我的输出中,父标记与包名称一起生成,也没有生成我想要的名称空间.谁能帮助我将平面文件转换为我期望的确切输出.

I'm trying to transform the pipe delimited string to xml, which is working fine. But, I'm not getting the actual output which I have shown below. In my output the parent tag is generating along with the package name, also its not generating the namespace which I want. Can you anyone help me how to transform the flat file to the exact output which I'm expecting.

CamelConfig.java

CamelConfig.java

@Component
public class CamelConfig extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        try {
            CamelContext context = new DefaultCamelContext();
            ConverterRoute route = new ConverterRoute();
            route.addRoutesToCamelContext(context);
            context.start();
            Thread.sleep(5000);
            context.stop();

        } catch (Exception exe) {
            exe.printStackTrace();
        }
    }
}

ConverterRoute.java

ConverterRoute.java

public class ConverterRoute implements RoutesBuilder {

    private static final String SOURCE_INPUT_PATH = "file://inbox?fileName=Source.txt";

    private static final String SOURCE_OUTPUT_PATH = "file://outbox?fileName=file.xml";

    public void addRoutesToCamelContext(CamelContext context) throws Exception {

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                try {
                    DataFormat bindyFixed = new BindyCsvDataFormat(Test.class);

                    from(SOURCE_INPUT_PATH).
                            unmarshal(bindyFixed).
                            marshal().
                            xstream().
                            to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Source.txt

Source.txt

55158|11901|2346
55158|11101|3454

Test.java

Test.java

package com.john;
@CsvRecord(separator = "\\|",skipField = true,name = "Test")
public class Test {

    @DataField(pos = 1,name = "ALT_NUM")
    private BigDecimal ALT_NUM;

    @DataField(pos = 2,name = "PRTNUM")
    private BigDecimal PRTNUM;

    @DataField(pos = 3,name = "UOMCOD")
    private Integer UOMCOD;

}

输出

OUTPUT

test.xml

test.xml

<?xml version='1.0' encoding='UTF-8'?>
    <com.john.Test>
            <ALT_NUM>55158</ALT_NUM>
            <PRTNUM>11901</PRTNUM>
            <UOMCOD>2346</UOMCOD>
    </com.john.Test>

预期产量

Expected Output

test.xml

test.xml

<?xml version='1.0' encoding='UTF-8'?>
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ALT_NUM>55158</ALT_NUM>
            <PRTNUM>11901</PRTNUM>
            <UOMCOD>2346</UOMCOD>
    </Test>

推荐答案

您可以使用 aliases 数据格式属性:

You can define type aliases with aliases dataformat property:

XStreamDataFormat xstream = new XStreamDataFormat();
xstream.setAliases(Collections.singletonMap("Test", Test.class.getCanonicalName()));
//...
.marshal(xstream)

为了定义自定义名称空间,请使用

In order to define custom namespace use @XStreamAsAttribute and @XStreamAlias in your entity:

public class Test {
    @XStreamAsAttribute
    @XStreamAlias("xmlns:xsi")
    private final String xmlns = "http://www.w3.org/2001/XMLSchema-instance";
    //...
}

这篇关于如何使用camel-xstream定义自定义名称空间和标签别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆