parseXML()
Description
Here is a little code that can transform an XML file into something that GScript can understand. The format is similar to that of ActionScript's XML Object.
XML.childNodes is an array of entities within the XML object.
XML.parent is the parent entity of the XML object.
XML.nodeName is the name of entity within the XML object.
XML.nodeType is an integer representing the type of the XML object (1 for plain text, 2 for XML entity).
XML.nodeValue is the XML object / plain text nested within the XML object.
XML.attributes is a string in the pattern of var1=val1&var2=val2&...&varN=valN. Variables can also be read with XML.attributes.var.
XML.xmlDecl is the XML declaration of the XML object.
XML.docTypeDecl is the DTD of the XML object.
Except nodeType, None of these values are read only.
The Code
PHP Code:
function parseXML(strXML) {
temp.xml = strXML;
this.childNodes = new [0];
temp.intOTag = temp.xml.pos("<");
temp.intETag = temp.xml.pos(">");
temp.intPrev = -1;
if (temp.intOTag == -1) {
this.childNodes.add(temp.xml);
this.childNodes[++temp.intPrev].type = 1;
} else {
while (temp.intOTag > -1) {
if (temp.intOTag > 0) {
this.childNodes.add(temp.xml.substring(0,temp.intOTag));
this.childNodes[++temp.intPrev].type = 1;
this.childNodes[temp.intPrev].parent = this;
temp.xml = temp.xml.substring(temp.intOTag);
temp.intOTag = temp.xml.pos("<");
temp.intETag = temp.xml.pos(">");
}
temp.strTag = temp.xml.substring(1,temp.intETag-1);
temp.strTagName = temp.strTag.substring(0,temp.strTag.pos(" ")).trim();
temp.strTag = temp.strTag.substring(temp.strTagName.length());
this.childNodes[temp.intPrev].nodeName = temp.strTagName;
if (!temp.strTagName.starts("?xml") && temp.strTagname != "!DOCTYPE" && !temp.strTagName.starts("!--") && !temp.strTagName.starts("!CDATA[")) {
if (temp.strTag != " /") {
temp.xml = temp.xml.substring(temp.intETag+1);
temp.strXMLCopy = temp.xml;
temp.intDepth = 1;
do {
temp.intOPos = temp.strXMLCopy.pos("<" @ temp.strTagName);
temp.intEPos = temp.strXMLCopy.pos("</" @ temp.strTagName @ ">");
if (temp.intOPos == -1) temp.intOPos = temp.strXMLCopy.length()+1;
switch (temp.intOPos < temp.intEPos) {
case true:
temp.intDepth ++;
temp.strXMLCopy = temp.strXMLCopy.substring(temp.strXMLCopy.substring(temp.intOPos+1).pos(">")+1);
break;
case false:
temp.intDepth --;
temp.strXMLCopy = temp.strXMLCopy.substring(temp.strXMLCopy.substring(temp.intEPos+1).pos(">")+1);
break;
}
} while (temp.intDepth > 0);
temp.strChild = temp.xml.substring(0,temp.intEPos);
temp.xml = temp.xml.substring(temp.intEPos+3+temp.strTagName.length());
this.childNodes.add("<" @ temp.strTagName @ temp.strTag @ ">" @ temp.strChild @ "</" @ temp.strTagName @ ">");
with(this.childNodes[++temp.intPrev]) parseXML(temp.strChild);
this.childNodes[temp.intPrev].type = 2;
this.childNodes[temp.intPrev].nodeName = temp.strTagName;
this.childNodes[temp.intPrev].parent = this;
this.childNodes[temp.intPrev].nodeValue = temp.strChild;
} else {
this.childNodes.add("<" @ temp.strTagName @ temp.strTag @ ">");
this.childNodes[++temp.intPrev].nodeName = temp.strTagName;
this.childNodes[temp.intPrev].nodeType = 2;
this.childNodes[temp.intPrev].parent = this;
temp.xml = temp.xml.substring(temp.intETag+1);
}
with(this.childNodes[temp.intPrev].attributes) getAttributes(temp.strTag);
} else {
switch (temp.strTagName) {
case "?xml":
this.xmlDecl = "<?xml" @ temp.strTag @ ">";
with (this.xmlDecl.attributes) getAttributes(temp.strTag);
break;
case "!DOCTYPE":
this.docTypeDecl = "<!DOCTYPE" @ temp.strTag @ ">";
break;
}
if (temp.strTagName == "!--") temp.intETag = temp.xml.pos("-->") + 2;
elseif (temp.strTagName == "!CDATA[") temp.intETag = temp.xml.pos("]>") + 1;
temp.xml = temp.xml.substring(temp.intETag+1);
}
temp.intOTag = temp.xml.pos("<");
temp.intETag = temp.xml.pos(">");
}
if (temp.xml.length() > 0) {
this.childNodes.add(temp.xml);
this.childNodes[temp.intPrev].type = 1;
this.childNodes[temp.intPrev].parent = this;
temp.xml = "";
}
}
return strXML;
}
function getAttributes(strTag) {
temp.arr = "";
while(strTag.length() > 0) {
strTag = strTag.trim();
temp.pos = strTag.pos("=");
if (temp.pos > -1) {
temp.var = strTag.substring(0,temp.pos);
if (strTag.charat(temp.pos+1) == "\"") temp.npos = strTag.substring(temp.pos+2).pos("\"");
else temp.npos = strTag.substring(temp.pos+2).pos(" ");
temp.val = strTag.substring(temp.pos+2,temp.npos);
this.(@temp.var) = temp.val;
temp.arr.add(temp.var @ "=" @ temp.val);
strTag = strTag.substring(temp.npos+temp.pos+3).trim();
} else strTag = "";
}
temp.len = temp.arr.size();
temp.str = temp.arr[0];
for (temp.i=1;temp.i<temp.len;temp.i++)
temp.str @= "&" @ temp.arr[temp.i];
this = temp.str;
return;
}
Usage
with(var) this = parseXML(strXML);
Parameters
strXML A string of XML or well-formed HTML.
Returns
The parameter passed to the function.
Example
PHP Code:
function onCreated() {
temp.query = requesturl("http://www.w3schools.com/xml/note.xml"); /* import file lines */
catchevent(temp.query,"onReceiveData","onData");
}
function onData(obj) {
with(me) this = parseXML(obj.fulldata); /* parse */
echo(me.xmlDecl.attributes.version); /* prints "1.0" */
}
The scene is set; to remake the XML file, you're going to have to get creative. Post problems / comments / etc.