xmlmirthe4x

Why can't I access xml message in Mirth?


I have a channel in Mirth which is receiving an XML message via HTTP, but when I try to access attributes and fields of the message, I cannot. If I copy and paste a message into "Send Message" it works, but if reprocess it does not work. In "Raw" it looks fine, but occasionally in "Transformed" it is xml encoded

Example message:

<?xml version="1.0" encoding="utf-8" ?>
<test attr="foo" />

Example message as shown in "Transformed":

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;test attr=&quot;foo&quot; /&gt;

Example javascript:

 channelMap.put('rootElementName', msg.localName());    // value is null
 channelMap.put('attrValue', msg['@attr'].toString());  // undefined is not XML

Solution

  • Two factors contribute to this sort of failure:

    Firstly, E4X does not support the <?xml version="1.0" encoding="utf-8" ?> declaration (bug 336551: You may get SyntaxError "xml is a reserved identifier"). You can fix it by adding a preprocessor to remove the declaration:

    return message.replace(/<\?xml[^>]*\?>/, "");
    

    To further confuse, the XML Declaration is hidden in the message viewer if you have "Format XML Message" checked.

    Secondly, if you are receiving the message from anything .Net, you likely have a byte-order mark at the front of the message. The first three bytes are likely nonprintable, and may need to be removed for the XML to parse in Mirth.

    Again, preprocessor to the rescue (Example 1, Example 2):

    return message.replace(/\uFEFF/g,'');