javaxmlapache-camelspring-camelbindy

How to define custom namespace and tag alias using camel-xstream


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

@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

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

55158|11901|2346
55158|11101|3454

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

<?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

<?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>

Solution

  • 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";
        //...
    }