The N/xml module provides utilities for building, parsing, serializing, and validating XML documents in SuiteScript. It is used for SOAP integrations, EDI file generation, and any workflow requiring structured XML output.
Parsing an XML String
define(["N/xml", "N/log"], (xml, log) => {
const xmlString = "<order><id>123</id><amount>999.99</amount></order>";
const doc = xml.Parser.fromString({ text: xmlString });
const idNode = doc.getElementsByTagName({ tagName: "id" })[0];
log.debug("Order ID", idNode.textContent);
return {};
});
Building XML with the DOM
const doc = xml.Document.create({ type: xml.NodeType.DOCUMENT_NODE });
const root = xml.Document.createElementNS({ document: doc, namespaceURI: null, qualifiedName: "orders" });
doc.appendChild({ newChild: root });
log.debug("XML output", xml.serialize({ node: doc }));
Key Objects
| Object / Method | Description |
|---|---|
xml.Parser.fromString() | Parse an XML string into a Document |
xml.serialize(options) | Serialize a DOM node back to XML string |
doc.getElementsByTagName() | Get elements by tag name |
xml.validate(options) | Validate XML against an XSD schema |