Adaptive XML
ai.xml
Method |
Parameters |
Returns |
Description |
---|---|---|---|
createFromUrl(url, method, body, headers, callbacks) available with 2018.3 |
url: String method: String body: String headers: String callbacks: Object |
ai.xml.sax | Creates an XML SAX parser object that can parse an XML stream. |
createFromUrlWithAuthorization(url, method, body, headers, callbacks) available with 2018.3 |
url: String method: String body: String headers: String callbacks: Object |
ai.xml.sax | Creates an XML SAX parser object that can parse an XML stream with authorization using OAuth. |
createParser() |
ai.xml.parser |
Creates a XML parser object that can parse XML strings. |
|
createDocument() |
ai.xml.document |
Creates a XML document. |
|
createDocType( xmlStr ) |
|
ai.xml.docType |
Create a doctype object that is added to a XML document to define its doctype. |
createElement( name ) |
|
ai.xml.element |
Create a XML element, which is the basic building block of a XML document, that can be added to an XMLdocument. |
createAttribute( name, value ) |
|
ai.xml.attribute |
Creates an attribute object that can be added to a XML element. |
Examples
// constructing an Intacct authentication XML request //'<request><control><senderid>adaptiveplanning</senderid><password>Bts9DrRkeZ</password><controlid>SomeRandomThingToMatchToResponse</controlid><dtdversion>3.0</dtdversion></control><operation><authentication><login><userid>Julia</userid><companyid>companyplanning-DEV</companyid><password>GhY93tYi</password></login></authentication></operation></request>' // create XML document var doc = ai.xml.createDocument(); // create root element and assign as the doc root var requestEl = ai.xml.createElement('request'); doc.setRootElement(requestEl); // control xml var controlEl = ai.xml.createElement('control'); var senderIdEl = ai.xml.createElement('senderid'); senderIdEl.setText('adaptiveplanning'); controlEl.addChildElement(senderIdEl); var controlPasswordEl = ai.xml.createElement('password'); controlPasswordEl.setText('Bts9DrRkeZ'); controlEl.addChildElement(controlPasswordEl); var controlIdEl = ai.xml.createElement('controlid'); controlIdEl.setText('SomeRandomThingToMatchToResponse'); controlEl.addChildElement(controlIdEl); var dtdEl = ai.xml.createElement('dtdversion'); dtdEl.setText('3.0'); controlEl.addChildElement(dtdEl); // operation xml //TODO
ai.xml.sax
Method |
Parameters |
Returns |
Description |
---|---|---|---|
createFromText() |
XML:string Encoding: UTF8 Callbacks: An object with functions |
ai.xml.sax | Creates an XML SAX parser object that can parse XML text. |
createFromUrl() |
url: String method: String body: String headers: String callbacks: An object with functions |
ai.xml.sax |
Creates an XML SAX parser object that can parse an XML stream. |
createFromUrlWithAuthorisation() |
url: String method: String body: String headers: String callbacks: An object with functions |
ai.xml.sax | Creates an XML SAX parser object that can parse an XML stream with authorization using OAuth |
The ai.xml.sax
examples below use callbacks
. A callback is a function that executes after another function finishes executing. One of the benefits of using callbacks with ai.xml.sax parsers is that you can catch errors and present those errors in the logs of a CCDS run for easier troubleshooting.
ai.xml.sax.createFromText example
var data = { books: [], currentTag: '' }; function createSaxParser(callbacks) { var xml ='<BookStore><book category="Cooking"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>$30.00</price></book><book category="Children"><title lang="en">Harry Potter</title><author>Norwegian: . French: </author><year>2015</year> <price>$50.00</price></book><book category="Sports"><title lang="en">Sports for Health</title><author>Šumbera</author><year>2016</year><price>$330.00</price></book> </BookStore>'; try { return ai.xml.sax.createFromText(xml, "UTF8", callbacks); } catch(exception) { ai.log.logError('Unable to setup sax parser', '' + exception); return null; } } function testConnection(context) { var parser = null; var callbacks = {}; var matched = false; callbacks.onOpenTag = function (tag) { if(tag.name === 'book') { matched = true; parser.stop(); } }; parser = createSaxParser(callbacks); if (parser !== null) { parser.readToEnd(); } return matched; } function importStructure(context) { var builder = context.getStructureBuilder(); var table = builder.addTable('MyNewTableFromText'); table.setDisplayName('MyNewTableFromText'); addColumn(table, 'book', 1, 'string', 100); addColumn(table, 'title', 2, 'string', 500); addColumn(table, 'author', 3, 'string', 100); addColumn(table, 'year', 4, 'string', 200); addColumn(table, 'price', 5, 'string', 1000) } var save = function (rowset, book) { var cells = []; cells.push(book.category); cells.push(book.title); cells.push(book.author); cells.push(book.year); cells.push(book.price); rowset.addRow(cells); }; function importData(context) { var rowset = context.getRowset(); rowset.setSmartParsingEnabled(true); var parser = null; var callbacks = {}; callbacks.onOpenTag = function (tag) { data.currentTag = tag.name; if(tag.name === 'book') { var book = { category: tag.attributes['category'] }; data.books.push(book); } }; callbacks.onText = function (text) { var current = data.books.length > 0 ? data.books[data.books.length-1] : null; if(data.currentTag === 'title') { current.title = text; } if(data.currentTag === 'author') { current.author = text; } if(data.currentTag === 'year') { current.year = text; } if(data.currentTag === 'price') { current.price = text; } }; callbacks.onCloseTag = function (tagname) { if(tagname === 'book') { var saving = data.books.pop(); save(rowset, saving); } }; parser = createSaxParser(callbacks); if (parser !== null) { parser.readToEnd(); } } function addColumn(table, columnName, order, type, length) { var newCol = table.addColumn(columnName); newCol.setDisplayName(columnName); newCol.setDisplayOrder(order); newCol.setIncludeByDefault(true); switch (type) { case 'string': newCol.setTextColumnType(length); newCol.setRemoteColumnType('string'); break; case 'datetime': newCol.setDateTimeColumnType(); newCol.setRemoteColumnType('datetime'); break; case 'float': newCol.setFloatColumnType(); newCol.setRemoteColumnType('float'); break; default: newCol.setTextColumnType(100); newCol.setRemoteColumnType('string'); } }
ai.xml.sax.createFromUrl example
function testConnection(context) { var response = null; var logId = 0; try { ai.log.logInfo(logId++ + ': Get a sax reader 1...'); parser = createSaxParser({}); if (parser !== null) { parser.stop(); ai.log.logInfo('Test connection result: success'); return true; } } catch (exception) { ai.log.logError('Test connection result: fail. ', '' + exception); } return false; } function importData(context) { var rowset = context.getRowset(); var columns = rowset.getColumns(); rowset.setSmartParsingEnabled(true); var parser = null; var rows = []; var count = 0; session = { currentNode: null, currentRow: null, reading: false, currentNodeLevel: 0, nodePath: new Array(40) }; var addRow = function () { var cells = []; cells.push(session.currentRow.id ? session.currentRow.id : ''); cells.push(session.currentRow.title ? session.currentRow.title : ''); cells.push(session.currentRow.updated ? session.currentRow.updated : ''); cells.push(session.currentRow.link ? session.currentRow.link : ''); cells.push(session.currentRow.summary ? session.currentRow.summary : ''); cells.push(session.currentRow['georss:point'] ? session.currentRow['georss:point'] : ''); cells.push(session.currentRow['georss:elev'] ? session.currentRow['georss:elev'] : ''); rowset.addRow(cells); }; var callbacks = {}; //An opening tag. Argument: object with name and attributes. callbacks.onOpenTag = function (tag) { session.currentNodeLevel++; session.nodePath[session.currentNodeLevel] = tag.name; if (tag.name == 'entry') { session.reading = true; session.currentRow = {}; return; } if (!session.reading) return; if (tag.name == 'link') { if (tag.attributes) { session.currentRow.link = tag.attributes["href"]; } } ai.log.logInfo(logId++ + ': <:' + tag.name + '>'); }; //Text node. Argument: string of text. callbacks.onText = function (text) { ai.log.logError(logId++ + ': ontext:' + text); if (!session.reading) return; var currentNode = session.nodePath[session.currentNodeLevel]; switch (currentNode) { case "id": case "title": case "updated": case "summary": case "georss:point": case "georss:elev": session.currentRow[currentNode] = text; break; default: break; } }; //A closing tag. callbacks.onCloseTag = function (tagname) { if (tagname == 'entry') { addRow(); session.reading = false; session.currentRow = {}; count++; if (count == 10) { //parser.Stop(); } } session.nodePath[session.currentNodeLevel] = null; session.currentNodeLevel--; return; }; //The text of a <![CDATA[ block. callbacks.onCData = function (data) { if (!session.reading || !data || data.length === 0) return; var currentNode = session.nodePath[session.currentNodeLevel]; if (currentNode == "summary") { session.currentRow.summary = data; } }; try { ai.log.logInfo(logId++ + ': Get a sax reader 1...'); parser = createSaxParser(callbacks); if (parser === null) { ai.log.logInfo(logId++ + ': Parser1 is null.'); return; } //var parser2 = createSaxParser({}); var extraParsers = []; var extraCount = 5; for (var i = 0; i < extraCount; i++) { ai.log.logInfo(logId++ + ': Creating a new parser.'); extraParsers[i] = createSaxParser({}, 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.atom'); ai.log.logInfo(logId++ + ': New parser created.'); } parser.readToEnd(); ai.log.logInfo(logId++ + ': Reading completed.'); } catch (exception) { // Example of logging to the CCDS log using the Error loglevel ai.log.logError('Get failed', '' + exception); return; } } function importStructure(context) { // Step 1: Get the structure builder. var builder = context.getStructureBuilder(); // Step 2: Create and configure tables and columns that represent // objects in the external system. var table = builder.addTable('StandardLoaderData'); table.setDisplayName('StandardLoaderData'); addColumn(table, 'id', 0, 'string', 100); addColumn(table, 'title', 1, 'string', 500); addColumn(table, 'updated', 2, 'string', 100); addColumn(table, 'link', 3, 'string', 200); addColumn(table, 'summary', 4, 'string', 1000); addColumn(table, 'georss:point', 5, 'string', 200); addColumn(table, 'georss:elev', 6, 'string', 200); } function previewData(context) { } function addColumn(table, columnName, order, type, length) { var newCol = table.addColumn(columnName); newCol.setDisplayName(columnName); newCol.setDisplayOrder(order); newCol.setIncludeByDefault(true); switch (type) { case 'string': newCol.setTextColumnType(length); newCol.setRemoteColumnType('string'); break; case 'datetime': newCol.setDateTimeColumnType(); newCol.setRemoteColumnType('datetime'); break; case 'float': newCol.setFloatColumnType(); newCol.setRemoteColumnType('float'); break; default: newCol.setTextColumnType(100); newCol.setRemoteColumnType('string'); } } var logId = 100001; function wait(ms) { var start = new Date().getTime(); var end = start; while (end < start + ms) { end = new Date().getTime(); } }; function createSaxParser(callbacks, url) { var defaultUrl = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.atom'; var u = url == null ? defaultUrl : url; var method = 'GET'; var body = ``; var headers = { }; ai.log.logInfo(logId++ + ': requesting sax parser from : ' + u); return ai.xml.sax.createFromUrl(u, method, body, headers, callbacks); };
ai.xml.parser
Method |
Parameters |
Returns |
Description |
---|---|---|---|
parse( xmlStr ) |
|
ai.xml.document |
Parses a valid XML document string into a XML document. |
Examples
var xmlString = '<request><control><senderid>adaptiveplanning</senderid><password>Bts9DrRkeZ</password><controlid>SomeRandomThingToMatchToResponse</controlid><dtdversion>3.0</dtdversion></control><operation><authentication><login><userid>Julia</userid><companyid>companyplanningDEV</companyid><password>GhY93tYi</password></login></authentication></operation></request>'; var parser = ai.xml.createParser(); var xmlDoc = parser.parse(xmlString);
ai.xml.document
Method |
Parameters |
Returns |
Description |
---|---|---|---|
clone() |
ai.xml.document |
Returns a clone of this document. |
|
getDOCTYPE() |
ai.xml.docType |
Gets the document DOCTYPE. |
|
getRootElement() |
ai.xml.element |
Gets the root element in the document. |
|
setDOCTYPE( docType ) |
|
Sets the document DOCTYPE. |
|
setRootElement( element ) |
|
Sets the document root element. |
|
toString() |
Provides a formatted string representation of the XML document. |
Examples
var doc = ai.xml.createDocument(); var docType = ai.xml.createDocType('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'); doc.setDOCTYPE(docType); var rootEl = ai.xml.createElement('Books'); doc.setRootElement(rootEl); alert(doc.toString()); // produces '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><Books></Books>';
ai.xml.docType
Method |
Parameters |
Returns |
Description |
---|---|---|---|
toString() |
String |
Provides a string representation of this document type declaration. |
Examples
var doc = ai.xml.createDocument(); var docType = ai.xml.createDocType('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'); doc.setDOCTYPE(docType); alert(docType.toString()); // produces '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
ai.xml.element
Method |
Parameters |
Returns |
Description |
---|---|---|---|
addChildElement( element ) |
|
Adds a child element. |
|
clone() |
ai.xml.element |
Returns a clone of this element. |
|
getAttribute( name ) |
name : String
|
ai.xml.attribute |
Gets the attribute of the given name. |
getAttributeAt( index ) |
index : Integer
|
ai.xml.attribute |
Gets the attribute at the given index of the attributes array for this element. |
getAttributes() |
Array<ai.xml.attribute> |
Gets the attributes of this element. |
|
getAttributeValue( name ) |
|
String |
Gets the value of the attribute of the given name. |
getChildElement( name ) |
|
ai.xml.element |
Gets the first child element having the specified given name. |
getChildElementAt( index ) |
|
ai.xml.element |
Gets a child element at the given index within the child elements. |
getChildElements() |
Array<ai.xml.elements> |
Gets the array of all child elements for this element. |
|
getChildElements( name ) |
|
Array<ai.xml.element> |
Gets the array of child elements for this element with the given name. |
getCommentText() |
String |
Gets the comment text. |
|
getName() |
String |
Gets the qualified name of this element. |
|
getText() |
String |
Gets the text of this element or CDATA. |
|
hasContents() |
Boolean |
Indicates whether or not this element has child contents: Text, CDATA or child elements. |
|
removeAllAttributes() |
Removes all attributes from this element. |
||
removeAttribute( name ) |
|
Removes the attribute of the given name in the element attributes array, and recompresses the array. |
|
removeChildElement( name ) |
|
Removes the first child element having the specified name. |
|
removeChildElementAt( index ) |
|
Removes a child element at the given index within the child elements. |
|
removeChildElements() |
Integer |
Removes all child elements from this element and returns count of elements removed. |
|
removeChildElements( name ) |
|
Integer |
Removes all child elements of the given element name from this element and returns count of elements removed. |
removeComments() |
Removes all comments attached directly to this element. |
||
removeText() |
Removes all text (including CDATA sections) attached directly to this element. |
||
setAttribute( attribute) |
|
Adds an attribute to this element. |
|
setAttribute(name, value ) |
|
Sets the value of the attribute of the given name, for this element. |
|
setCDATAText( text ) |
|
Removes all existing content (except comments) from this element, including child elements, and then sets CDATA section text for this element. |
|
setCommentText( text ) |
|
Removes all comments attached directly to this element, and then adds a new comment with the given text. |
|
setName( name ) |
|
Sets the name of this element. |
|
setText( text ) |
|
Removes all existing content (except comments) from this element, including child elements, and then sets the element text. |
|
toString() |
Provides a formatted string representation of this element. |
Examples
var tablesElement = ai.xml.createElement('Tables'); var table1Element = ai.xml.createElement('Table'); table1Element.setAttribute('remoteId','123'); table1Element.setText('This is some text'); tablesElement.addChildElement(table1Element); alert(tablesElement.toString()); // produces '<tables><table remoteId="123">This is some text</table></tables>'
ai.xml.attribute
Method |
Parameters |
Returns |
Description |
---|---|---|---|
clone() |
ai.xml.attribute |
Returns a clone of this attribute. |
|
getName() |
String |
Gets the name of the attribute. |
|
getValue() |
String |
Gets the value of the attribute. |
|
setName( name ) |
|
Sets the name of the attribute. |
|
setValue( value ) |
|
Sets the value of the attribute. |
|
toString() |
Returns a string representation of this attribute. |
Examples
var attribute1 = ai.xml.createAttribute('age', '30'); var attribute2 = attribute1.clone(); attribute2.setName('height'); attribute2.setValue('1.8'); alert(attribute1.toString()); // produces - 'age="30"' alert(attribute2.toString()); // produces - 'height="1.8"'