message.addChild¶
Signature: message.addChild(nodePath, childNode, value*, index*)
Returns: void
Add the specified childNode to the first node that matches nodePath.
Note
This method only applies to the following formatted messages: HL7, ASTM, EDIFACT, X12, XML.
Parameters¶
| Type | Name | Description | Default |
|---|---|---|---|
| String | nodePath | the node path (XPath, HPath, Column ID, etc.) | |
| String | childNode | the name of the child node to add | |
| String | value* | (optional) the value of the child node to add | null |
| Integer | index* | (optional) the index of the child node relative to the other child nodes (0 = first child) | 0 |
Examples¶
hl7
// assumes `message` is an HL7 message model object, like:
// MSH|^~\&|||||||ORU^R01|12345
// PID|||10001||Doe^John
// For HL7, addChild must use the root node '/'
message.addChild('/', 'OBR', 'OBR|1|||88304^PATHOLOGY');
// You can also specify the index
message.addChild('/' , 'OBX', 'OBX|1|TX|TEST1||First observation' , 3);
message.addChild('/' , 'OBX', 'OBX|3|TX|TEST3||Third observation' , 5);
message.addChild('/' , 'OBX', 'OBX|4|TX|TEST4||Fourth observation', 6);
message.addChild('/' , 'OBX', 'OBX|2|TX|TEST2||Second observation', 4);
message.addChild('/' , 'OBX', 'OBX|5|TX|TEST5||Fifth observation' , 7);
/* resulting message:
MSH|^~&|||||||ORU^R01|12345
PID|||10001||Doe^John
OBR|1|||88304^PATHOLOGY
OBX|1|TX|TEST1||First observation
OBX|2|TX|TEST2||Second observation
OBX|3|TX|TEST3||Third observation
OBX|4|TX|TEST4||Fourth observation
OBX|5|TX|TEST5||Fifth observation
*/
xml
// Given an XML message like:
message = qie.createXMLMessage(
`
<person>
<name>Nate</name>
<age>30</age>
</person>
`
);
// Add a child node to the /person element
message.addChild(
"/person", // nodePath
"favorite_dessert", // childNode
"ice cream" // value
);
// Resulting XML:
// <person>
// <name>Nate</name>
// <age>30</age>
// <favorite_dessert>ice cream</favorite_dessert>
// </person>
// --------------------------------------------------
// Example using all parameters (nodePath, childNode, value, index)
// --------------------------------------------------
// Given an XML message like:
message = qie.createXMLMessage(
`
<people>
<person>
<name>Nate</name>
<age>30</age>
</person>
<person>
<name>John</name>
<age>41</age>
</person>
<person>
<name>Jane</name>
<age>52</age>
</person>
</people>
`
);
// Insert attributes into the <person name=[John]> at specific positions
message.addChild("/people/person[name='John']", "favorite_dessert", "ice cream", 1);
message.addChild("/people/person[name='John']", "car", "porsche", 2);
message.addChild("/people/person[name='John']", "spouse", "heidi", 1);
message.addChild("/people/person[name='John']", "company", "Qvera", 3);
// Resulting XML:
// <people>
// <person>
// <name>Nate</name>
// <age>30</age>
// </person>
// <person>
// <spouse>heidi</spouse>
// <favorite_dessert>ice cream</favorite_dessert>
// <company>Qvera</company>
// <car>porsche</car>
// <name>John</name>
// <age>41</age>
// </person>
// <person>
// <name>Jane</name>
// <age>52</age>
// </person>
// </people>