Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   Assigning Arbitrary Empty XML Node (http://www.mirthcorp.com/community/forums/showthread.php?t=219099)

ace 04-18-2019 08:07 AM

Assigning Arbitrary Empty XML Node
 
Is it possible to create an arbitrary empty/blank XML node to assign to the arrays that Connect uses to represent HL7?

For example, I can hardcode a statement like this which replaces the MSH:

var msgXML = new XML(SerializerFactory.getSerializer('HL7V2').toXML (inStr));
msgXML['PID']['PID.5'] = <PID.5/>;

But, the <PID.5/> seems like it is a special variable or constant in Connect, not a String, so is there a way to have a variable that contains the
name of an HL7 field, like the String, 'MSH.10', or in the case above, 'PID.5', and by using that String variable, dynamically assign
an empty node, as I have done in the hardcoded version? Perhaps something like this:

var hl7FieldName = 'PID.5';
var emptyXmlNode = magicMysteryFunction(hl7FieldName);
msgXML['PID']['PID.5'] = emptyXmlNode;

agermano 04-18-2019 09:16 AM

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

ace 04-30-2019 02:18 PM

Assigning Arbitrary Empty XML Node
 
Thanks for the ideas, agermano. I am just getting back to this.

This approach is the most useful for me. My previous report that it did not work was the result of a bad test. User error. / My Bad.:

var msgXML = new XML(SerializerFactory.getSerializer('HL7V2').toXML (connectorMessage.getRawData().toString()));
var segment = 'PID';
var hl7FieldName = 'PID.7';
msgXML[segment][hl7FieldName] = <{hl7FieldName}/>;
var neutralizedMessage = SerializerFactory.getSerializer('HL7V2').fromXML(m sgXML.toString());//THE SPACE BETWEEN m AND s IS ACTUALLY NOT THERE!!!


But for replacing an arbitrarily deep field, all but the 3rd one, require me to access the Array of Arrays with hard-coded depths of reference, for example, where msgXML[segName][fieldName] is 2 levels deep. Since I want to write a function that can take an arbitrary field or subfield name, I can imagine parsing the field name and using the 3rd approach to build a copy of the msgXML Array, one field at a time, in a series of loops, and then convert that edited copy back to XML, but that seems clunky, and probably slow-performing, so I was hoping there was a way to build a string that could be used as a path into the array (or into the XML) that could be used as an index for the edit.

agermano 05-10-2019 08:59 AM

Each approach has different benefits and drawbacks. You might have to use multiple approaches in a generalized function depending on initial state and desired outcome.

For example, what do you do when a parent, or more critically a segment, doesn't exist? By default new segments will be created at the end of the message. What do you do when existing segments are repeating? Are the repeating segments consecutive? What do you do when existing fields are repeating?

If you have a very strict outcome in mind, you may want to build a new message (using the outbound template) from pieces of the inbound message rather than trying to delete everything you don't need.

ace 05-17-2019 09:17 AM

Assigning Arbitrary Empty XML Node
 
That is a good point. In my case, I just want to know that there is no data in the node my function is told to blank-out.

So, if there are child nodes, it is fine to clobber them. And, if there is a missing parent, that implies there is no data in the subject node, so in that case, there is no change to make on the XML.


All times are GMT -8. The time now is 03:44 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation