DOM manipulation is sometimes a bit painful. The <b>DomUtil</b> helper provides a few convenience functions
const DomUtil = sdk.DomUtil;
or
const DomUtil = client.DomUtil;
Create DOM from XML string:
const doc = DomUtil.parse(`<root> <one/> </root>`);
Writes a DOM document or element as a string:
const s = DomUtil.toXMLString(docOrElement);
Creates a new document
const queryDoc = DomUtil.newDocument("queryDef");
Escape text value
const escaped = DomUtil.escapeXmlString(value);
Find element by name (finds the first element with given tag). This is a very common operation when manipulating Campaign XML documents
const el = DomUtil.findElement(parentElement, elementName, shouldThrow);
Get the text value of an elemennt. This will accomodate text elements, cdata elements, as well has having multiple text child element (which is ususally not the case in Campaign)
const text = DomUtil.elementValue(element);
Iterates over child elements
var child = DomUtil.getFirstChildElement(parentElement); while (child) { ... child = DomUtil.getNextSiblingElement(child); }
Iterates over child elements of a given type
var methodChild = DomUtil.getFirstChildElement(parentElement, "method"); while (methodChild) { ... methodChild = DomUtil.getNextSiblingElement(methodChild, "method"); }
Get typed attribute values, with automatic conversion to the corresponding xtk type, and handling default values
const stringValue = DomUtil.getAttributeAsString(element, attributeName) const byteValue = DomUtil.getAttributeAsByte(element, attributeName) const booleanValue = DomUtil.getAttributeAsBoolean(element, attributeName) const shortValue = DomUtil.getAttributeAsShort(element, attributeName) const longValue = DomUtil.getAttributeAsLong(element, attributeName)
JSON to XML conversion (SimpleJson by default)
const document = DomUtil.fromJSON(json); const json = DomUtil.toJSON(documentOrElement);
BadgerFish can be forced as well
const document = DomUtil.fromJSON(json, "BadgerFish"); const json = DomUtil.toJSON(documentOrElement, "BadgerFish");