Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Easy Xml, dot reader
cybernetix
post Dec 15 2004, 11:01 AM
Post #1


ReloadeD
Group Icon
Group: Honored
Posts: 562
Joined: 20-May 04
From: Belgrade, Serbia
Member No.: 17





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 bigwink.gif


you can download source files HERE

file: 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.
 
+Quote Post  Go to the top of the page
cmonney
post Dec 15 2004, 11:07 AM
Post #2


~ Soul Rebel ~
Group Icon
Group: Main Team*
Posts: 3,625
Joined: 18-May 04
From: a mind-altering psychedelic trip
Member No.: 3





QUOTE(cybernetix @ Dec 15 2004, 02:01 PM)
Tired of siblings, nodes, childs etc ?
Most of users would probably say: Yes !



that's me...


Thanks a lot!!!


Tip-Hat.gif
 
+Quote Post  Go to the top of the page
*D*
post Dec 15 2004, 06:26 PM
Post #3


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,990
Joined: 18-May 04
From: North Pole
Member No.: 2





Thanks Nemo, saw this earlier today but never had time to reply. Downloaded to have a look and try to understand this better. smile.gif
 
+Quote Post  Go to the top of the page
RedDragon
post Dec 16 2004, 03:18 AM
Post #4


insert coin
Group Icon
Group: Main Team
Posts: 5,344
Joined: 24-May 04
From: Maastricht
Member No.: 35





well thx!

my skills are way below this so it would need some work from my side to understand this smile.gif

thx for adding !
 
+Quote Post  Go to the top of the page
chriskanye
post Feb 12 2006, 10:45 PM
Post #5


Getting Used To The Dots
Group Icon
Group: Member
Posts: 31
Joined: 12-February 06
Member No.: 9,709





Kool!
 
+Quote Post  Go to the top of the page
hi_selamlar
post Jan 9 2007, 08:13 AM
Post #6


New Member
Group Icon
Group: Member
Posts: 3
Joined: 5-January 07
Member No.: 14,404





nice sample

thanks. bigwink.gif
 
+Quote Post  Go to the top of the page
surreal64
post Apr 22 2007, 12:37 AM
Post #7


New Member
Group Icon
Group: Member
Posts: 5
Joined: 20-April 07
Member No.: 15,934





nice sample

thanks. bigwink.gif
 
+Quote Post  Go to the top of the page
eskim
post May 30 2007, 12:48 PM
Post #8


New Member
Group Icon
Group: Member
Posts: 4
Joined: 30-May 07
Member No.: 16,320





Thx, great thing is that you covered any depth.
Pozdrav iz Crne Gore.
 
+Quote Post  Go to the top of the page
Migs723
post Jun 22 2007, 03:51 AM
Post #9


New Member
Group Icon
Group: Member
Posts: 2
Joined: 22-June 07
Member No.: 16,609





Hi Guys,
I'm still a learner with this, I have a xml file that I want to retrive certain parts in " " into a field in swish, but I don't know how to do that. Can anyone help me please, I would realy appriciate it.
Thanks
Migs723
 
+Quote Post  Go to the top of the page
Ruben
post Jul 6 2007, 07:37 AM
Post #10


New Member
Group Icon
Group: Member
Posts: 4
Joined: 5-July 07
Member No.: 16,774





Hi!, Thanks for this great job!!
 
+Quote Post  Go to the top of the page

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 29th July 2010 - 11:09 PM
Home Insurance | Cheap Travel Insurance | Cheat Codes | Home Insurance | Computer Jobs