Tired of siblings, nodes, childs etc ?
Most of users would probably say: Yes !
Is there any easyier way to read XML ?
Read this thread and see for yourself.
-
As much as I can see, most of users
avoids to use XML couse it is
not
quite friendly so I tried to make it easy with
dot reader, couse it is
fammiliar to most of swishmax users.
So, how it works ?
Suppose that you have XML file like this:
CODE
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
<nodeOne>node-1</nodeOne>
<nodeTwo nodeTwoAttr1="node-2-Attr-1" nodeTwoAttr2="node-2-Attr-2">
<subnodeTwo1>sub-Node-2-1</subnodeTwo1>
<subnodeTwo2 subnodeTwo2Attr1="sub-Node-2-2-Attr-1" />
<subnodeTwo3 subnodeTwo3Attr1="sub-Node-2-3-Attr-1">sub-Node-2-3</subnodeTwo3>
</nodeTwo>
<nodeOne nodeOneAttr1="second-node-1-Attr-1">node-1-second-one</nodeOne>
<nodeOne nodeOneAttr1="third-node-1-Attr-1">node-1-third-one</nodeOne>
<nodeThree>
<subnodeThree1>
<subSubnodeThree11>sub-Sub-Node-3-1-1</subSubnodeThree11>
</subnodeThree1>
</nodeThree>
<nodeOne>fourth-node-1</nodeOne>
</test>
CODE
var sourcefile="cbx-xml_dot_reader_test.xml";
var xm = new XML();
xm.load(sourcefile);
xm.ignoreWhite=true; Â
After XML is loaded to var xm, you can search it like this:
to retrieve nodeOne name
xm.test.nodeOne.nodNam()
to retrieve nodeOne value
xm.test.nodeOne.nodVal()
to retrieve nodeTwo name
xm.test.nodeTwo.nodNam()
to retrieve nodeTwo value
xm.test.nodeTwo.nodVal()
to retrieve name of 1st attribute in nodeTwo
xm.test.nodeTwo.attNam(1) or xm.test.nodeTwo.attNam() // couse 1st is default
to retrieve value of 1st attribute in nodeTwo
xm.test.nodeTwo.attNam(1) or xm.test.nodeTwo.attNam() // couse 1st is default
Also, you can retrieve nodes and values with numbers of their positions or
occurrence number, like this:
to retrieve name of 1st node
xm.test.__1.nodNam()
to retrieve value of 1st node
xm.test.__1.nodVal()
to retrieve name of 1st attribute in 2nd node
xm.test.__2.attNam(1)
to retrieve value of 1st attribute in 2nd node
xm.test.__2.attVal(1)
also you can retrieve it like this
xm.test.__2.__1.nodVal()
it is the same as
xm.test.nodeTwo.__1.nodVal()
or
xm.test.nodeTwo.subnodeTwo1.nodVal()
you can retrieve value of attribute by attribute name
xm.test.nodeTwo.attVal('nodeTwoAttr2')
etc ...
This prototype will read XML to any depth.
Now,
pay attention that nodeOne appears 4 times, so if you want to retrieve
3rd occurrence of nodeOne, you should do this:
xm.test.nodeOne__3.nodNam()
xm.test.nodeOne__3.nodVal()
There are many possibilities to retrieve XML, and most of them you will find in
cbx-xml_dot_reader_test.swi file.
source file for dot reader is atached, but here you can see source as well:
CODE
/*
XML dot reader.
[b]This source / file is free for personal use ONLY. You may not sell or use it for
profit purposes of any kind unless donation is made. You can change file for
personal use, but if you want to re/distribute it, it MUST be in original form
with this text included.[/b]
File created by cybernetix, main team member on: http://13dots.com
e-mail: krhkojeznanje@hotmail.com
site: http://cybernetix.t35.com Â
*/
onload() {
XMLNode.prototype.__resolve=function(nName) {
var i,n=this.childNodes,nLength=n.length,arr;
if ((arr=nName.split("__"))[1]) {
var x=number(arr[1])-1;
if (arr[0]=="") return n[x];
var c=0,nName=arr[0];
for(i=0;i<nLength;i++) if(n[i].nodeName==nName) if(c++==x) return n[i];
} else {
for(i=0;i<nLength;i++) if(n[i].nodeName==nName) return n[i];
}
};
XMLNode.prototype.nodVal=function() {
return this.firstChild.nodeValue;
};
XMLNode.prototype.nodNam=function() {
return this.nodeName;
};
XMLNode.prototype.attVal=function(x) {
var i;
if (typeOf(x)=="number") {
var c=0, n=x-1;
for(i in this.attributes) if(c++==n) return this.attributes[i];
} else if (x!=undefined) {
return eval("this.attributes."+x);
} else {
for(i in this.attributes) return this.attributes[i];
}
};
XMLNode.prototype.attNam=function(x) {
var i,c=0,n=(x==undefined)?0:(x-1);
for (i in this.attributes) if (c++==n) return i;
};
stop();
}
that's it, a few rows of code, simple but works. happy
SWiSHing
you can download source files HEREfile: cbx-xml_dot_reader_clean.zip, contains:1. cbx-xml_dot_reader_test.html // html file
2. cbx-xml_dot_reader_test.swi // source file that will load files 3 & 4 and will read XML.
2a.cbx-xml_dot_reader_test.swf // swf file that will load files 3 & 4 and will read XML.
3. cbx-xml_dot_reader_clean.swi // source file with dot reader prototype
3a.cbx-xml_dot_reader_clean.swf // swf file with dot reader prototype
4. cbx-xml_dot_reader_test.xml // xml file with test content
note: all swf, xml and html files must be in the same folder for test.