Codice ActionScript
xmlData = new XML();
xmlData.ignoreWhite = true;
idnews = new Array();
notizia = new Array();
xmlData.onLoad = parse_data;
xmlData.load("Ciao.xml");
function parse_data(success) {
if (success == true) {
xmlData = parseXmlToArray(xmlData);
} else {
trace("Error:\nThe XML document did not load properly.\n");
}
}
function multiNodeObject() {
}
multiNodeObject.prototype = new Object();
multiNodeObject.prototype.length = function() {
var count = 0;
for (var name in this) {
var addCount = true;
for (var i = 0; i<name.length; i++) {
if (name.charCodeAt(i)<48 || name.charCodeAt(i)>57) {
addCount = false;
}
}
if (addCount) {
count++;
}
}
return count;
};
function parseXmlToArray(xm) {
// temporary object that will be returned
var temp_obj = new Object();
// Checks to see if the current xml object has any children. If it doesn't, There isn't data here and the function will end.
if (xm.hasChildNodes() && xm.childNodes.length>1) {
if (xm.nodeName != null) {
temp_obj[xm.nodeName] = new multiNodeObject();
for (var i = 0; i<xm.childNodes.length; i++) {
temp_obj[xm.nodeName][i] = parseXmlToArray(xm.childNodes[i]);
}
if (xm.attributes) {
for (var name in xm.attributes) {
temp_obj[xm.nodeName][name] = xm.attributes[name];
}
}
} else if (xm.nodeName == null) {
for (var i = 0; i<xm.childNodes.length; i++) {
temp_obj[xm.childNodes[i].nodeName] = new multiNodeObject();
for (var k = 0; k<xm.childNodes[i].childNodes.length; k++) {
temp_obj[xm.childNodes[i].nodeName][k] = parseXmlToArray(xm.childNodes[i].childNodes[k]);
}
if (xm.childNodes[i].attributes) {
for (var name in xm.childNodes[i].attributes) {
temp_obj[xm.childNodes[i].nodeName][name] = xm.childNodes[i].attributes[name];
}
}
}
}
} else if (xm.hasChildNodes() && xm.childNodes.length == 1) {
if (xm.firstChild.nodeType == 1) {
temp_obj[xm.firstChild.nodeName] = new multiNodeObject();
for (var j = 0; j<=xm.childNodes.length; j++) {
temp_obj[xm.firstChild.nodeName][j] = parseXmlToArray(xm.firstChild.childNodes[j]);
}
if (xm.firstChild.attributes) {
var temp1 = new Object();
for (var name in xm.firstChild.attributes) {
temp_obj[xm.firstChild.nodeName][name] = xm.firstChild.attributes[name];
}
}
} else if (xm.firstChild.nodeType == 3) {
var temp2 = new Object();
temp2["value"] = xm.firstChild.nodeValue;
if (xm.attributes) {
for (var name in xm.attributes) {
temp2[name] = xm.attributes[name];
}
}
//else if(i=2){null;}
temp_obj[xm.nodeName] = temp2;
}
}
// Outputs the current nodes' attribuets array to the xml_data textbox
return temp_obj;
}
Questo invece il mio XML:
<?xml version="1.0" encoding="UTF-8"?>
<notizie>
<notizia id="1">
<titolo>Cagliari</titolo>
<sottotitolo>Roma</sottotitolo>
<testo>Ciao mi chiamo Vito e sto provando ancora il mio XML</testo>
<foto>vito.jpg</foto>
</notizia>
<notizia id="2">
<titolo>Cagliari</titolo>
<sottotitolo>Roma</sottotitolo>
<testo>Ciao mi chiamo Francesco e sto provando ancora il mio XML</testo>
<foto>peppa.jpg</foto>
</notizia>
<notizia id="3">
<titolo>MA dai</titolo>
<sottotitolo>ma che sta a fa</sottotitolo>
<testo>questo e in grassetto sono andato a capo</testo>
<foto>rocco.jpg</foto>
</notizia>
</notizie>
Sapreste dirmi come faccio a prendere i valori "id notizia", "titolo", "sottotitolo", "testo" e buttarli in un campo di testo?