123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: Viewer
- *| (C) Copyright IBM Corp. 2001, 2014
- *|
- *| US Government Users Restricted Rights - Use, duplication or
- *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *|
- *+------------------------------------------------------------------------+
- */
- function XMLParser(s, parent)
- {
- if (s == null) {
- return null;
- }
- if (/^\s*</.test(s)) {
- // remove leading spaces
- s = s.replace(/^\s*/, '');
- if (s.charAt(1) == '/') {
- // end of an element
- var reParent = new RegExp("^</" + parent.getName() + "\\s*>", "gi");
- if (!reParent.test(s)) {
- alert("invalid XML " + parent.getName() + "\n" + s);
- return null;
- }
- return XMLParser(s.replace(RegExp.lastMatch,""), parent.parentNode);
- }
- else {
- // new element
- var reNewElement = /^\s*<([\w:\-_\.]+)/;
- if ( reNewElement.test(s) ) {
- var nodeName = RegExp.$1;
- var e = new XMLElement(nodeName, parent);
- var patternToRemove = new RegExp("^<" + nodeName + "[^>]*>");
- s = s.replace(patternToRemove, "");
- var currentNodeText = RegExp.lastMatch;
- var attributesPattern = /([\w:\-_\.]+)="([^"]*)"/gi;
- var attrMatches = currentNodeText.match(attributesPattern);
- if (attrMatches != null) {
- for (var i=0; i<attrMatches.length; i++) {
- var attrText = attrMatches[i];
- (/([\w:\-_\.]+)\s*=\s*"(.*)"/).test(attrText);
- e.setAttribute(RegExp.$1, RegExp.$2);
- }
- }
- if ( ! (/\/>$/).test(currentNodeText) ) {
- XMLParser(s, e);
- return e;
- }
- else {
- XMLParser(s, parent);
- return e;
- }
- }
- }
- }
- else {
- // text node
- if (s && parent)
- {
- var textNodePattern = new RegExp("([^<]*)</" + parent.getName() + "\\s*[^>]*>", "gi");
- textNodePattern.test(s);
- var nodeValue = RegExp.$1;
- parent.setValue(nodeValue);
- return (XMLParser(s.replace(nodeValue, ""), parent));
- }
- }
- return null;
- }
- function XMLElement(s, parent)
- {
- this.nodeName = s;
- this.nodeValue = "";
- this.attributes = [];
- this.childNodes = [];
- this.parentNode = parent;
- if (this.parentNode) {
- this.parentNode.appendChild(this);
- }
- }
- XMLElement.prototype.appendChild = function (e) { this.childNodes[this.childNodes.length] = e; };
- XMLElement.prototype.hasChildNodes = function ()
- {
- if (this.childNodes.length > 0) {
- return true;
- }
- else {
- return false;
- }
- };
- XMLElement.prototype.findChildByName = function (n, deepWalk)
- {
- if (this.getName() == n) {
- return(this);
- }
- for (var i = 0; i < this.childNodes.length; i++) {
- if (this.childNodes[i].getName() == n) {
- return this.childNodes[i];
- }
- }
- if (deepWalk != false) {
- for (i = 0; i < this.childNodes.length; i++) {
- var foundChild = this.childNodes[i].findChildByName(n, deepWalk);
- if (foundChild) {
- return foundChild;
- }
- }
- }
- return null;
- };
- XMLElement.prototype.findChildWithAttribute = function (attr, val)
- {
- for (var i = 0; i < this.childNodes.length; i++) {
- if (this.childNodes[i].getAttribute(attr) == val) {
- return this.childNodes[i];
- }
- }
- return null;
- };
- XMLElement.prototype.getElementsByTagName = function (s, deep)
- {
- var a = [];
- for (var i = 0; i < this.childNodes.length; i++) {
- if (this.childNodes[i].getName() == s) {
- a[a.length] = this.childNodes[i];
- }
- }
- if (deep != false) {
- for (i = 0; i < this.childNodes.length; i++) {
- var aChild = this.childNodes[i].getElementsByTagName(s);
- for (var j = 0; j < aChild.length; j++) {
- a[a.length] = aChild[j];
- }
- }
- }
- return a;
- };
- XMLElement.prototype.getName = function () { return this.nodeName; };
- XMLElement.prototype.getValue = function () { return this.nodeValue; };
- XMLElement.prototype.setAttribute = function (a, v) { this.attributes["_" + a] = v; };
- XMLElement.prototype.setValue = function (v) { this.nodeValue = v; };
- XMLElement.prototype.getAttribute = function (a)
- {
- var retval = "";
- if (typeof sXmlDecode == "function")
- {
- retval = sXmlDecode(this.attributes["_" + a]);
- }
- else
- {
- retval = this.attributes["_" + a];
- }
- return (retval == null ? "" : retval);
- };
- XMLElement.prototype.toString = function()
- {
- var s = "<" + this.getName();
- for (var i in this.attributes) {
- s += " " + i.substring(1) + "=\"" + this.attributes[i] + "\"";
- }
- s += ">" + this.getValue();
- for (var j = 0; j < this.childNodes.length; j++) {
- s += this.childNodes[j].toString();
- }
- s += "</" + this.getName() + ">";
- return s;
- };
- function XMLBuilderLoadXMLFromString (markup, forceDOMParserIfAvailable)
- {
- var xmlDocument = null;
- if (typeof DOMParser != 'undefined')
- {
- xmlDocument = new DOMParser().parseFromString(markup, 'application/xml');
- }
- else if (typeof ActiveXObject != 'undefined')
- {
- try
- {
- xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
- xmlDocument.loadXML(markup);
- }
- catch (e)
- {
- }
- }
- return xmlDocument;
- }
- function XMLBuilderCreateXMLDocument (rootElementName, namespaceURI, documentType)
- {
- var xmlDocument = null;
- namespaceURI = namespaceURI || '';
- documentType = documentType || null;
- if (document.implementation && document.implementation.createDocument)
- {
- if (typeof namespaceURI == "undefined") {
- namespaceURI = "http://www.w3.org/2000/xmlns/";
- }
- xmlDocument = document.implementation.createDocument(namespaceURI, rootElementName, documentType);
- }
- else if (typeof ActiveXObject != 'undefined')
- {
- try
- {
- xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
- var rootElement = xmlDocument.createNode(1, rootElementName, namespaceURI);
- xmlDocument.appendChild(rootElement);
- }
- catch (e)
- {
- }
- }
- return xmlDocument;
- }
- function XMLBuilderCreateElementNS (namespaceURI, elementName, ownerDocument)
- {
- var element = null;
- if (typeof ownerDocument.createElementNS != 'undefined')
- {
- if (typeof namespaceURI == "undefined") {
- namespaceURI = "http://www.w3.org/2000/xmlns/";
- }
- element = ownerDocument.createElementNS(namespaceURI, elementName);
- }
- else if (typeof ownerDocument.createNode != 'undefined')
- {
- element = ownerDocument.createNode(1, elementName, namespaceURI);
- }
- return element;
- }
- function XMLBuilderSetAttributeNodeNS (element, attributeName, attributeValue, namespaceURI)
- {
- if (typeof element.setAttributeNS != 'undefined')
- {
- if (typeof namespaceURI == "undefined") {
- namespaceURI = "http://www.w3.org/2000/xmlns/";
- }
- element.setAttributeNS(namespaceURI, attributeName, attributeValue);
- }
- else if (typeof element.ownerDocument != 'undefined' && typeof element.ownerDocument.createNode != 'undefined')
- {
- var attribute = element.ownerDocument.createNode(2, attributeName, namespaceURI);
- attribute.nodeValue = attributeValue;
- element.setAttributeNode(attribute);
- }
- }
- function XMLBuilderSerializeNode (node)
- {
- var sXml = "";
- if (typeof XMLSerializer != 'undefined')
- {
- try
- {
- sXml = new XMLSerializer().serializeToString(node);
- }
- catch (e)
- {
- //Can end up here if the node was not an XML object
- }
- }
- else if (typeof node == "object" && typeof node.xml != 'undefined')
- {
- sXml = node.xml;
- }
- return sXml.replace(/^\s+/g, "").replace(/\s+$/g, "");
- }
- function XMLHelper_GetText(node, bDeepWalk)
- {
- var text="";
- var childNodes = node.childNodes;
- for(var i = 0; i < childNodes.length; ++i)
- {
- if(childNodes[i].nodeType == 3)
- {
- text += childNodes[i].nodeValue;
- }
- else if (childNodes[i].nodeName == "Value")
- {
- text += childNodes[i].getAttribute("display");
- }
- else if (bDeepWalk)
- {
- text += XMLHelper_GetText(childNodes[i], true);
- }
- }
- return text;
- }
- function XMLHelper_GetLocalName(node)
- {
- if(typeof node.baseName != "undefined") {
- return node.baseName;
- }
- return node.localName;
- }
- function XMLHelper_FindChildByTagName(node, name, deepWalk)
- {
- // validate deep walk argument (must be "true" or "false")
- if (typeof deepWalk == "undefined" || (deepWalk != true && deepWalk != false)) {
- deepWalk = true;
- }
- if (XMLHelper_GetLocalName(node) == name)
- {
- return(node);
- }
- var i;
- for (i = 0; i < node.childNodes.length; i++)
- {
- if (XMLHelper_GetLocalName(node.childNodes[i]) == name)
- {
- return node.childNodes[i];
- }
- }
- if (deepWalk != false)
- {
- for (i = 0; i < node.childNodes.length; i++)
- {
- var foundChild = XMLHelper_FindChildByTagName(node.childNodes[i], name, deepWalk);
- if (foundChild)
- {
- return foundChild;
- }
- }
- }
- return null;
- }
- function XMLHelper_FindChildrenByTagName(oNode, sName, bDeepWalk)
- {
- // validate deep walk argument (must be "true" or "false")
- if (typeof bDeepWalk == "undefined" || (bDeepWalk != true && bDeepWalk != false))
- {
- bDeepWalk = true;
- }
- var aFoundNodes = [];
- var oNodeChildren = oNode.childNodes;
- for (var idxChildNodes = 0; idxChildNodes < oNodeChildren.length; idxChildNodes++)
- {
- if (XMLHelper_GetLocalName(oNodeChildren[idxChildNodes]) == sName)
- {
- aFoundNodes[aFoundNodes.length] = oNodeChildren[idxChildNodes];
- }
- if (bDeepWalk === true)
- {
- var oFoundChildren = XMLHelper_FindChildrenByTagName(oNodeChildren[idxChildNodes], sName, bDeepWalk);
- if (oFoundChildren.length > 0)
- {
- aFoundNodes = aFoundNodes.concat(oFoundChildren);
- }
- }
- }
- return aFoundNodes;
- }
- function XMLHelper_GetFirstChildElement( oEl )
- {
- var oChild = null;
- if ( oEl && oEl.childNodes && oEl.childNodes.length )
- {
- for ( var i = 0; i < oEl.childNodes.length; i++ )
- {
- if ( oEl.childNodes[i].nodeType == 1 )
- {
- oChild = oEl.childNodes[i];
- break;
- }
- }
- }
- return oChild;
- }
- function XMLHelper_FindChildrenByAttribute(oNode, sName, sValue, bDeepWalk, bGetOnlyFirstChild)
- {
- // validate deep walk argument (must be "true" or "false")
- if (typeof bDeepWalk == "undefined" || (bDeepWalk != true && bDeepWalk != false))
- {
- bDeepWalk = true;
- }
- if (typeof sValue != "string" && typeof sValue != "number")
- {
- sValue = null;
- }
- else
- {
- sValue = sValue.toString();
- }
- var aFoundNodes = [];
- var oNodeChildren = oNode.childNodes;
- for (var idxChildNodes = 0; idxChildNodes < oNodeChildren.length; idxChildNodes++)
- {
- var oChild = oNodeChildren[idxChildNodes];
- if (oChild.nodeType == 1)
- {
- var sNodeAttr = oChild.getAttribute(sName);
- if (sNodeAttr !== null)
- {
- if (sValue === null || sNodeAttr == sValue)
- {
- if (bGetOnlyFirstChild)
- {
- return [oChild];
- }
- else
- {
- aFoundNodes[aFoundNodes.length] = oChild;
- }
- }
- }
- if (bDeepWalk === true)
- {
- var oFoundChildren = XMLHelper_FindChildrenByAttribute(oChild, sName, sValue, bDeepWalk, bGetOnlyFirstChild);
- if (oFoundChildren.length > 0)
- {
- if (bGetOnlyFirstChild)
- {
- if (oFoundChildren.length == 1)
- {
- return oFoundChildren;
- }
- else
- {
- return [oFoundChildren[0]];
- }
- }
- else
- {
- aFoundNodes = aFoundNodes.concat(oFoundChildren);
- }
- }
- }
- }
- }
- return aFoundNodes;
- }
- /*
- Here are a few examples of how to use the XMLBuilder:
- First you need to create an XML Document. This document INCLUDES the root node of the XML.
- var rootElement = XMLBuilderCreateXMLDocument("root", "");
- --> Here we're not specifying a default nameSpaceURI or documentType, these are optional
- *** We now have created the following XML:
- <root/>
- Next, you can either create a node in the document using the document element you created above
- var node1Element = rootElement.createElement("node1");
- OR you can create a element based on a namespace using the document element (rootElement in our case).
- var node1Element = XMLBuilderCreateElementNS("http://someNamespace", "node1", rootElement);
- Next, you can set attributes on the element
- node1Element.setAttribute("attrX", "valY");
- OR you can set namespaces on the element (or both of course)
- XMLBuilderSetAttributeNodeNS(node1Element, "xmlns:xs", "http://www.w3.org/2001/XMLSchema", "");
- --> Here we don't need to define the final parameter as it's part of the xmlns namespace.
- Note that if you declare a namespaces on the element which is not an "xmlns", you need to specify the namespace URI of that namespace.
- XMLBuilderSetAttributeNodeNS(node1Element, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
- --> In this case, since we uses "xsi", instead of "xmlns", we had to provide the final parameter.
- --> Note that when declaring the final parameter, you don't need to define "xsi" as an xmlns, it's included for free
- Next, you need to append the child node to the document node. Note that for children of the document element,
- you need to append as a child of the "documentElement", not the rootElement.
- rootElement.documentElement.appendChild(node1Element);
- For every other element you create (other than document elements) you can just use the following.
- node1Element.appendChild(node2Element);
- *** We now have created the following XML:
- <root>
- <node1 attrX="valY" xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:xsi="http://www.w2.org/2001/XMLSchema-instance" xsi:type="SOAP-ENC:Array"/>
- </root>
- --> Note that the xmlns for xsi is automatically included for free.
- */
|