简体   繁体   中英

How to generate custom tag names and namespaces in xml using apache camel

I'm trying to transform pipe delimited string data to xml using camel bindy. But it is generating the tags along with the class name. Also I would like to add namespace to my tags.

I tried to use Camel process to generate custom tag, it's not working.

ConverterRoute.java


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

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

    public void addRoutesToCamelContext(CamelContext context) throws Exception {
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                try {
                    DataFormat bindyFixed = new BindyCsvDataFormat(PartInboundIFD.class);

                    NameSpace nameSpace = new NameSpace("PART_INB_IFD","https://apache.org.com");
                    from(SOURCE_INPUT_PATH).
                            unmarshal(bindyFixed).
                            marshal().
                            xstream().
                            to(SOURCE_OUTPUT_PATH);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Pojo.java

@CsvRecord(separator = "\\|",skipField = true)
public class Pojo {

    @Link
    private ControlSegment CONTROL_SEGMENT;

}

CamelComponent.java

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();
        }
    }
}

OUTPUT Result.xml

<list>
    <com.abc.domain.Pojo>
        <CONTROL__SEGMENT/>
            <TRNNAM>PART_TRAN</TRNNAM>
            <TRNVER>9.0</TRNVER>
    </com.abc.domain.Pojo>
</list>

Above posted is the output of the given transformation.In the first tag it is printing the tag name with whole package and class name(eg: com.abc.domain.Pojo).Also I'm trying to generate namespace its not generating that in my output.

May be you can add an additional XSLT route ( https://camel.apache.org/components/latest/xslt-component.html ). Within the XSLT it's possible to transform the XML to your liking and add the correct namespaces ( How can I add namespaces to the root element of my XML using XSLT? )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM