简体   繁体   中英

Camel JSON to XML conversion issue

If I convert a JSON to XML with a default namespace using Camel unmarshal and XmlJsonDataFormat , all the opening xml elements have a space at the end. I didn't find an possibility to affect this behavior.

This is the converted XML:

<Message xmlns="http://example.com/Message/1">
    <Header >
        <Header1 >header 1</Header1>
        <Header2 >header 2</Header2>
    </Header>
</Message>

This is my route definition:

public void configure() throws Exception {
    // create JSON to XML data format
    XmlJsonDataFormat json2XmlFormat = new XmlJsonDataFormat();
    json2XmlFormat.setEncoding("UTF-8");
    json2XmlFormat.setRootName("Message");

    List<XmlJsonDataFormat.NamespacesPerElementMapping> namespaces = new        ArrayList<XmlJsonDataFormat.NamespacesPerElementMapping>();
    namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "||http://example.com/Message/1|"));
    json2XmlFormat.setNamespaceMappings(namespaces);

    from("direct:input")
        .unmarshal(json2XmlFormat)
        .log("${body}");
}

The converted XML is still valid with the spaces. But I would like to have a solution with out them.

setSkipWhitespace() and setTrimSpaces() are only for XML to JSON

Any idea?

I got the result without spaces in the tags if the namespace is defined without the | characters (this is equivalent to no namespace):

namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "http://example.com/Message/1"));

Or if a proper alias for the namespace is defined such as ns :

namespaces.add(new XmlJsonDataFormat.NamespacesPerElementMapping("Message", "|ns|http://example.com/Message/1|"));    

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