Skip to content

message.setNode

Signature: message.setNode(nodePath, value, instance*, repetition*, forceNode*)

Returns: void

Find the node instance that matches nodePath (or create the node if it doesn't exist and forceNode = true) and set its value.

Note

Calling message.setNode(nodePath, value) without the instance parameter sets the first node instance that matches nodePath.

Note

On a DICOM tag whose value representation is Other Byte (OB), Other Double (OD), Other Float (OF), Other Word (OW) or Unknown (UN), the value must be Base64 encoded and is refused if it is not. Use Set DICOM Bytes to pass a byte array instead.

Note

This method only applies to the following formatted messages: HL7, ASTM, CSV, DICOM, EDIFACT, Old Fxd Width, Fxd Length, ISO 8583, JSON, Text, X12, XML.

Parameters

Type Name Description Default
String nodePath the node path (XPath, HPath, Column ID, etc.)
String value the new node value
Integer instance* (optional) the instance to return: segment for HL7, ASTM, EDIFACT, or X12; row for CSV or fixed-length formats; or match for DICOM, JSON, or XML. Use 1 for the first instance. 1
Integer repetition* (optional) the field repeat instance for HL7 or ASTM to return. Use 1 for the first instance. 1
Boolean forceNode* (optional) create the node if it doesn't exist true

Examples

hl7

// Example: Set existing nodes, create missing nodes (forceNode=true), and target a specific instance.

// assumes `message` is an HL7 message model object, like:
//    MSH|^~\&|LAB|HOSPITAL|EHR|HOSPITAL|20260317103000||ORU^R01|MSG00001|P|2.5
//    OBR|1||ORDER123|88304^Lab Panel^L
//    OBX|1|NM|GLU^Glucose^L||105|mg/dL|70-110|N|||F
//    OBX|2|NM|WBC^White Blood Cell Count^L||7.2|10*3/uL|4.0-11.0|N|||F
//    OBX|3|ST|COVID^COVID Result^L||Negative|||N|||F

// Set the first OBX-5 value
message.setNode('OBX-5', 'REDACTED');

// Create PID-5 (Patient Name) if PID segment doesn't exist
message.setNode(
   'PID-5',       // nodePath
   'Doe^John',    // value
   1,             // instance
   true           // create if it doesn't exist
);

/* result:
MSH|^~\&|LAB|HOSPITAL|EHR|HOSPITAL|20260317103000||ORU^R01|MSG00001|P|2.5
OBR|1||ORDER123|88304^Lab Panel^L
OBX|1|NM|GLU^Glucose^L||REDACTED|mg/dL|70-110|N|||F
OBX|2|NM|WBC^White Blood Cell Count^L||7.2|10*3/uL|4.0-11.0|N|||F
OBX|3|ST|COVID^COVID Result^L||Negative|||N|||F
PID|||||Doe^John
*/

json

// Example: Set existing nodes, create missing nodes (forceNode=true), and target a specific instance.

// Sample JSON message structure
message = qie.createJSONMessage(
   '{' +
   '"observations": [' +
   '{ "code": "GLU", "value": 105 },' +
   '{ "code": "WBC", "value": 7.2 },' +
   '{ "code": "COVID", "value": "Negative" }' +
   ']' +
   '}'
);

// Set the first observation value
message.setNode('/observations/value', '144');

// Create a description field on the second observation value
message.setNode(
   '/observations/[code=WBC]/description',   // nodePath
   'White Blood Cell Count',                 // value
   1,                                        // instance
   true                                      // create it if it doesn't exist
);
/* result:
{
  "observations": [
    {
      "code": "GLU",
      "value": "144"
    },
    {
      "code": "WBC",
      "value": 7.2,
      "description": "White Blood Cell Count"
    },
    {
      "code": "COVID",
      "value": "Negative"
    }
  ]
}
*/

xml

// Example: Set existing nodes, create missing nodes (forceNode=true), and target a specific instance.

// Sample XML message structure
message = qie.createXMLMessage(`<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book>
        <title>Learning JavaScript</title>
        <author>John Doe</author>
        <price>39.99</price>
    </book>
    <book>
        <title>Advanced JavaScript</title>
        <author>Jane Smith</author>
        <price>49.99</price>
    </book>
</bookstore>`);

// Update an existing node
var priceBefore = message.getNode("/bookstore/book[1]/price"); // "39.99"
message.setNode("/bookstore/book[1]/price", "29.99");
var priceAfter = message.getNode("/bookstore/book[1]/price");  // "29.99"

// Create a missing node and set its value
message.setNode(
    "/bookstore/book[2]/publisher", // node path
    "TechBooks Inc.",               // value
    1,                              // instance
    true                            // create node if it doesn't exist
);
var publisher = message.getNode("/bookstore/book[2]/publisher"); // "TechBooks Inc."

// Set the value on a specific instance of a repeating node
message.setNode(
    "/bookstore/book/author", // node path
    "Jean Val Jean",          // value
    2                         // instance (second <author>)
);
var firstAuthor = message.getNode("/bookstore/book[1]/author");  // "John Doe"
var secondAuthor = message.getNode("/bookstore/book[2]/author"); // "Jean Val Jean"