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

TechGeek

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove namespace prefix from xml using Xstream?

How to omit a tag with Xstream?

How to define an empty custom HTML tag?

how to remove default namespaces and add custom namespace in root tag of xml using C#?

How do I define a variable in a namespace with "using namespace"?

How to define a route by using camel-quarkus-jdbc?

How to define namespace in python?

How to define an alias for a command with options in a Powershell Profile using functions?

XStream ForbiddenClassException when using a custom Forager with OptaPlanner

Using Subqueries to Define Column Alias

How to Alias a namespace and extend the original namespace based on the alias

How to define a custom function of an instance using setattr

how to parse namespace tag associated with unique value using python BeautifulSoup

Using "prop" namespace in camel blueprint to resolve a property

How to define an alias in fish shell?

How to define a property alias in JS?

Using namespace in django for anchor tag

how to define a label by a tag

Using a custom registry in a camel application

How can use a custom folder asset using namespace routes?

Camel XStream unmarshall attributes are null

How to add alias for extension namespace in wikimedia?

How to parse XML to java object using XStream

How to generate/ serialize a Xml using XStream?

C Using #define to point to or alias a function

Using bashrc to define an alias with loop in Ubuntu

Namespace alias inside a namespace

How to define an Apache Camel route to InfluxDB

Using define directive to get the namespace name