web stats
Mirth Community - View Single Post - Assigning Arbitrary Empty XML Node
View Single Post
  #2  
Old 04-18-2019, 09:16 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 802
agermano is on a distinguished road
Default

You can do it with e4x templating like this:
Code:
var hl7FieldName = 'PID.5';
msgXML['PID'][hl7FieldName] = <{hl7FieldName}/>;
The magical mystery function is a mirth helper function called createSegment.
Code:
var hl7FieldName = 'PID.5';
var emptyXmlNode = createSegment(hl7FieldName);
msgXML['PID']['PID.5'] = emptyXmlNode;
You can add the parent object and it will insert it for you, but this will only replace the first occurrence if the field or segment is repeating. The previous two examples will replace a repeating PID.5 field with a single empty occurence.
Code:
var hl7FieldName = 'PID.5';
var emptyXmlNode = createSegment(hl7FieldName, msgXML['PID']);
And if you know that msgXML['PID']['PID.5'] refers to a single node that already exists in the message, you can do this to empty it. It will complain that setChildren isn't a function if msgXML['PID']['PID.5'] doesn't exist or returns multiple nodes.
Code:
msgXML['PID']['PID.5'].setChildren(new XMLList());
Reply With Quote