javaxmlapache-camelbindy

How to handle Underscores in xml tags


How to handle the underscores in xml tags. Here is the code which I'm working. It was generating me the xml tag with double underscores ("__") instead of single underscore ("_"). Can anyone help me to overcome this issue.

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_$simple{date:now:yyyyMMddHHmmssSSS}.xml";

    public void addRoutesToCamelContext(CamelContext context) throws Exception {

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

                    XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
                    xStreamDataFormat.setAliases(Collections.singletonMap("TEST_INB",Test.class.getCanonicalName()));

                    from(SOURCE_INPUT_PATH).
                            split().tokenize(System.lineSeparator()).
                            unmarshal(bindyFixed).
                            marshal(xStreamDataFormat).
                            to(SOURCE_OUTPUT_PATH).log("Finished Transformation").end();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

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

Test.java

@CsvRecord(separator = "\\|",skipField = true,name = "TEST_INB")
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;

}

Source.txt

55158|11901|2346
55158|11101|3454

OUTPUT

File.xml

<?xml version='1.0' encoding='UTF-8'?>    
    <TEST__INB>
            <ALT__NUM>55158</ALT__NUM>
            <PRTNUM>11901</PRTNUM>
            <UOMCOD>2346</UOMCOD>
    </TEST__INB>

Expected OUTPUT

File.xml

<?xml version='1.0' encoding='UTF-8'?>    
    <TEST_INB>
            <ALT_NUM>55158</ALT_NUM>
            <PRTNUM>11901</PRTNUM>
            <UOMCOD>2346</UOMCOD>
    </TEST_INB>

Solution

  • XStream is internally using underscore to escape characters when producing the XML.

    See FAQ: http://x-stream.github.io/faq.html#XML_double_underscores

    You may try to use a custom driver (rather than the default one):

    XmlFriendlyNameCoder nameCoder = new XmlFriendlyNameCoder("_-", "_");  
    Dom4JDriver myCustomDriver = new Dom4JDriver(nameCoder);
    ... 
    XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
    xStreamDataFormat.setDriver(myCustomDriver);
    

    or, if not working, maybe try with this other alternate driver:

    DomDriver myCustomDriver = new DomDriver("UTF_8", new NoNameCoder());
    

    Hope this works !