PDA

View Full Version : help me with xml


javiadas
03-17-2005, 11:01 AM
im very bad with xml, can someone help me to parse something like this ??


<?xml version="1.0" encoding="utf-8" ?>
<discografia>
<album id="1" nombre="Yo te necesito">
<cancion id="1" pista="Qué lástima" audio="d1c1.mp3" />
<cancion id="2" pista="Las musiqueras" audio="N/A" />
<cancion id="3" pista="Mi gran verdad" audio="N/A" />
<cancion id="4" pista="Que pensabas" audio="N/A" />
<cancion id="5" pista="A mi ley" audio="N/A" />
<cancion id="6" pista="Yo te necesito" audio="d1c6.mp3" />
<cancion id="7" pista="El castigo de quererte" audio="N/A" />
<cancion id="8" pista="Te esperaré" audio="d1c8.mp3" />
<cancion id="9" pista="No le hago al barro" audio="N/A" />
<cancion id="10" pista="Una vez más" audio="N/A" />
</album>
<album id="2" nombre="12 éxitos norteños">
<cancion id="1" pista="A mi ley" audio="N/A" />
<cancion id="2" pista="Mi error" audio="N/A" />
<cancion id="3" pista="Si te pasa lo que me pasó" audio="N/A" />
<cancion id="4" pista="Una vida de amor" audio="N/A" />
<cancion id="5" pista="En un rato más" audio="N/A" />
<cancion id="6" pista="Volvamos a intentar" audio="N/A" />
<cancion id="7" pista="Tienes razón" audio="N/A" />
<cancion id="8" pista="Quisiera mejor morir" audio="N/A" />
<cancion id="9" pista="Las holgazanas" audio="N/A" />
<cancion id="10" pista="Los alambrados" audio="N/A" />
<cancion id="11" pista="Mi chaparrita" audio="N/A" />
<cancion id="12" pista="Los chicanos" audio="N/A" />
</album>
</discografia>


thanks... i hope someone can help me.

12051
03-17-2005, 11:48 AM
Maybe something like this:



your_xml= new XML();
your_xml.ignoreWhite = true;
your_xml.onLoad = function() {
trace(this)
var firstalbum = this.firstChild.childNodes[0];

}
your_xml.load("pathtoyourxmlfile.xml");

javiadas
03-17-2005, 12:00 PM
ok more or less, anyother help ??

thanks man

javiadas
03-17-2005, 12:19 PM
thanks man.. now i get it...

thanks alot...

12051
03-17-2005, 01:22 PM
No problem...good luck with your application.

javiadas
03-17-2005, 01:32 PM
thank, i have a trouble here, can you someone help me ??


var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success) {
xml_parser();
};
my_xml.load("discografia.xml");

var albums:Object = new Object();

function xml_parser(){
var total_discos = my_xml.firstChild.childNodes.length;
for (i=0;i<total_discos;i++){
var album = my_xml.firstChild.childNodes[i];
var ident = album.attributes
albums[i+1] = {id:ident.id, nombre:ident.nombre};
for (var aNode:XMLNode = my_xml.firstChild.childNodes[i].firstChild; aNode != null; aNode = aNode.nextSibling) {
id = aNode.attributes.id
pista = aNode.attributes.pista
audio = aNode.attributes.audio
}
}
variables();
}

function variables(){
for (var i in albums[1]) {
trace("albums."+i+" = "+albums[i]);
}
}


well the thing is that i cant se inside my ibject for example albums[1].id or something like that and it says that its undefined.

can someone help me ??

thanks

12051
03-17-2005, 01:34 PM
the correct syntax is:


albums[1].attributes.id

javiadas
03-17-2005, 01:39 PM
hmmm ... ??
what im trying to do is to make albums an array of object, is that ok ??
then i have albums[1].id and albums[1].name and all the array but when i try to see the value its says that is undefined

12051
03-17-2005, 01:41 PM
because you must use albums[1].attributes.id and albums[1].attributes.name

that's just the way it works bro!

javiadas
03-17-2005, 01:43 PM
ok man let me try it, thanks

12051
03-17-2005, 01:44 PM
I can't see the problem in your code though....

javiadas
03-17-2005, 01:49 PM
if i run this script with the complete xml i generate the result is:

albums.id = undefined
albums.nombre = undefined

12051
03-17-2005, 01:54 PM
maybe replace this:
var ident = album.attributes
with:
var ident = album[1].attributes

or

this:
var album = my_xml.firstChild.childNodes[i];
with:
var album = my_xml.firstChild.childNodes;