Camel XML tokenizer handling newlines within XML element tags

Solution Unverified - Updated -


  • JBoss Fuse 6.0


There is an issue MR-785 [1] with XML tokenizer for Camel with version < 12.0.2. The issue is that if tokenized XML element tag contains newline, Camel won't parse this element properly. For example in older versions of Camel the following route snippet...


.. won't properly parse the following XML message...

  <Child A="1"



The recommended solution is to upgrade JBoss Fuse to version 6.1 (which includes proper version of Apache Camel with MR-785 [1] fixed).

If upgrading Fuse version is not possible, you can try to workaround this issue by preprocessing XML message before it is consumed by Camel splitter. The snippet below demonstrates how to use javax.xml.transform.Transformer API to remove newlines from within XML element tags.

import org.apache.camel.Body;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

public class XmlElementNormalizationTransformer {

    public String process(@Body InputStream xml) throws Exception {
        StringWriter transformedXml = new StringWriter();
        Source source = new StreamSource(xml);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(source, new StreamResult(transformedXml));
        return transformedXml.toString();


You can wire the mentioned preprocessor into your route using bean [2] component, as demonstrated on the snippet below.


In the attachment you can find minimal working Maven project demonstrating the preprocessing from the snippets above.


Root Cause

The old implementation of XML tokenizer was using single-line regexp pattern to parse XML elements tags. As a result Camel was unable to properly handle the multi-line XML element tags. Fixed version of Camel parses newline characters in element tags correctly.


This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.