Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    lettura xml tramite javascript

    salve a tutti ho un problema con la lettura di un file xml. Praticamente io dovrei leggere il file xml per estrarre i dati per poi poterli utilizzare per creare un immagine
    questo è il file xml
    Codice PHP:
    <?xml version='1.0' encoding='ISO-8859-1'?> 
    <voltaCeleste> 
    <costellazione> 
    <nome>Cancro</nome> <
    nomelatino>Cancer</nomelatino> 
    <abbreviazione>Cnc</abbreviazione> 
    <coordinate> 
    <AscensioneRetta>9h</AscensioneRetta> 
    <Declinazione>10g</Declinazione> 
    <AreaTotale>506 gradi quadrati</AreaTotale> 
    </coordinate> 
    <stella> 
    <nomeStella>Al Tarf</nomeStella> 
    <AR>8h 16m 30.95s</AR> 
    <Dec>+9g 11m 8.40s</Dec> 
    <magnitudineA>3.53</magnitudineA> 
    </stella> 
    <stella>
     <nomeStella>Asellus Australis</nomeStella> 
    <AR>08h 44m 41.11s</AR> 
    <Dec>+18g 9m 47.50s</Dec> 
    <magnitudineA>3.94</magnitudineA> 
    </stella> 
    <stella> 
    <nomeStella>Iota cnc</nomeStella> 
    <AR>8h 46m 41.83s</AR> 
    <Dec>+28g 45m 36.00s</Dec> 
    <magnitudineA>4.03</magnitudineA> 
    </stella> 
    <stella> 
    <nomeStella>Iota Cancri</nomeStella>
    <AR>8h 46m 41.83s</AR> 
    <Dec>+28g 45m 36.00s</Dec> 
    <magnitudineA>4.03</magnitudineA> 
    </stella> 
    <stella> 
    <nomeStella>Acubens</nomeStella> 
    <AR>08h 58m 29.20s</AR> 
    <Dec>+11g 51m 28.00s</Dec> 
    <magnitudineA>4.26</magnitudineA> 
    </stella> 
    <stella>
    <nomeStella>Asellus Borealis</nomeStella> 
    <AR>8h 43m 17.21s</AR> 
    <Dec>+21g 28m 6.90s</Dec> 
    <magnitudineA>4.66</magnitudineA> 
    </stella> 
    </costellazione> 
    <costellazione> 
    <nome>Capricorno</nome> 
    <nomelatino>Capricornus</nomelatino> 
    <abbreviazione>Cpr</abbreviazione> 
    <coordinate> 
    <AscensioneRetta>21h</AscensioneRetta> 
    <Declinazione>-20g</Declinazione> 
    <AreaTotale>414 gradi quadrati</AreaTotale> 
    </coordinate> 
    <stella> 
    <nomeStella>Dabih</nomeStella> 
    <AR>20h 21m 00.67s</AR> 
    <Dec>-14g 46m 52.92s</Dec> 
    <magnitudine>3.08</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Deneb Algedi</nomeStella> 
    <AR>326h 04m 15s</AR> 
    <Dec>-16g 21m 19s</Dec> 
    <magnitudine>2.98</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Nashira</nomeStella> 
    <AR>324h 19m 51s</AR> 
    <Dec>-16g 53m 21s</Dec> 
    <magnitudine>3.80</magnitudine> 
    </stella> 
    </costellazione> 
    <costellazione> 
    <nome>Pegaso</nome> 
    <nomelatino>Pegasus</nomelatino> 
    <abbreviazione>Peg</abbreviazione> 
    <coordinate> 
    <AscensioneRetta>23h</AscensioneRetta> 
    <Declinazione>+15g</Declinazione> 
    <AreaTotale>1121 gradi quadrati</AreaTotale> 
    </coordinate> 
    <coordinate> 
    <AscensioneRetta>23h</AscensioneRetta> 
    <Declinazione>+15g</Declinazione> 
    <AreaTotale>1121 gradi quadrati</AreaTotale> 
    </coordinate> 
    <stella> 
    <nomeStella>Enif</nomeStella> 
    <AR>21h 44m 11.2s</AR> 
    <Dec>+9g 52m 30.0s</Dec> 
    <magnitudine>2.38</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Scheat</nomeStella> 
    <AR>23h 03m 46.45s</AR> 
    <Dec>28g 04m 58.04s</Dec> 
    <magnitudine>2.42</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Markab</nomeStella> 
    <AR>23h 04m 45.66s</AR> 
    <Dec>+15g 12m 18.93s</Dec> 
    <magnitudine>2.49</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Algenib</nomeStella> 
    <AR>00h 13m 14.2s</AR> 
    <Dec>+15g 11m 01s</Dec> 
    <magnitudine>2.83</magnitudine> 
    </stella> 
    </costellazione> 
    <costellazione> 
    <nome>Orsa Maggiore</nome> 
    <nomelatino>UrsaMajor</nomelatino> 
    <abbreviazione>OMax</abbreviazione> 
    <coordinate> 
    <AscensioneRetta>10h</AscensioneRetta> 
    <Declinazione>+55g</Declinazione> 
    <AreaTotale>1280 gradi quadrati</AreaTotale> 
    </coordinate> 
    <stella> 
    <nomeStella>Alioth</nomeStella> 
    <AR>12h 54m 01.74s</AR> 
    <Dec>+55g 57m 35.36s</Dec> 
    <magnitudine>1.77</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Dubhe</nomeStella> 
    <AR>11h 03m 43.7s</AR> 
    <Dec>+61g 45m 03s</Dec> 
    <magnitudine>1.79</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Alkaid</nomeStella> 
    <AR>13h 47m 32.4s</AR> 
    <Dec>+49g 18m 48s</Dec> 
    <magnitudine>1.85</magnitudine> 
    </stella> </costellazione> 
    <costellazione> 
    <nome>Orsa Minore</nome> 
    <nomelatino>UrsaMinor</nomelatino> 
    <abbreviazione>OMin</abbreviazione>
    <coordinate> 
    <AscensioneRetta>15h</AscensioneRetta> 
    <Declinazione>+70g</Declinazione> 
    <AreaTotale>256 gradi quadrati</AreaTotale> 
    </coordinate> 
    <stella> 
    <nomeStella>Polaris</nomeStella> 
    <AR>02h 31m 48.7s</AR> 
    <Dec>+89g 15m 51s</Dec> 
    <magnitudine>1.97</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Kochab</nomeStella> 
    <AR>14h 50m 42.3s</AR> 
    <Dec>+74g 09m 20s</Dec> 
    <magnitudine>2.07</magnitudine> 
    </stella> 
    <stella> 
    <nomeStella>Pherkad</nomeStella> 
    <AR>15h 20m 43.75s</AR> 
    <Dec>+71g 50m 02.3s</Dec> 
    <magnitudine>3.00</magnitudine> 
    </stella> 
    </costellazione> 
    </voltaCeleste>
    io, con il seguente codice, riesco a far leggere tutte le costellazioni con relative coordinate ma quando vado a far leggere le stelle me ne legge una sola per costellazione
    Codice PHP:
    function carica(urlhandler) { 
    if ( 
    document.implementation && document.implementation.createDocument ) { 
    var 
    xml document.implementation.createDocument(""""null); 
    xml.onload = function() { 
    handler(xml); 
    }
    xml.load(url); 

    else if ( 
    window.ActiveXObject ) { 
    var 
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.onreadystatechange = function() { 
    if ( 
    xml.readyState == ) { 
    handler(xml); 


    xml.load(url); 



    function 
    lettura(xml) { 
    var 
    table document.getElementById("t1"); 
    var 
    cost xml.getElementsByTagName("costellazione");
     var 
    coor xml.getElementsByTagName("coordinate"); 
    for (var 
    0cost.lengthi++) { 
    var 
    nome cost[i].getElementsByTagName("nome")[0].firstChild.data
    var 
    nLat cost[i].getElementsByTagName("nomelatino")[0].firstChild.data
    var 
    abb cost[i].getElementsByTagName("abbreviazione")[0].firstChild.data
    var 
    asc cost[i].getElementsByTagName("AscensioneRetta")[0].firstChild.data
    var 
    dec cost[i].getElementsByTagName("Declinazione")[0].firstChild.data
    var 
    are cost[i].getElementsByTagName("AreaTotale")[0].firstChild.data
    var 
    ste cost[i].getElementsByTagName("nomeStella")[0].firstChild.data

    var 
    row table.insertRow(1); // + 1 = Non considero la testata della tabella row.insertCell(0).appendChild(document.createTextNode(nome)); row.insertCell(1).appendChild(document.createTextNode(nLat)); row.insertCell(2).appendChild(document.createTextNode(abb)); row.insertCell(3).appendChild(document.createTextNode(asc)); row.insertCell(4).appendChild(document.createTextNode(dec)); row.insertCell(5).appendChild(document.createTextNode(are)); 
    var row2 table.insertRow3); row2.insertCell(0).appendChild(document.createTextNode(ste)); 


    la parte della tabella mi serve solo per vedere se i dati vengo letti tutti correttamente

    in attesa di una risposta grazie dell'attenzione

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Magari ti può essere utile convertire il file letto in un oggetto javascript.
    Immaginando di caricare in una variabile di nome doc il file XML restituito tramite responseXML dell'oggetto XMLHttpRequest, la variabile xml del codice seguente conterrà il tuo documento convertito in un oggetto (secondo la convenzione di Parker, più o meno)&hellip;
    Può darsi che avere a che fare con un oggetto ti possa semplificare la vita&hellip;

    codice:
    function buildValue(sValue) {
    	if (/^\s*$/.test(sValue)) { return(null); }
    	if (/^(true|false)$/i.test(sValue)) { return(sValue.toLowerCase() === "true"); }
    	if (isFinite(sValue)) { return(parseFloat(sValue)); }
    	if (isFinite(Date.parse(sValue))) { return(new Date(sValue)); }
    	return(sValue);
    }
    
    function getXMLData (oXMLParent) {
    	var vResult = /* true is the default value for empty nodes */ true, nLength = 0, sTxtContent = "";
    	if (oXMLParent.hasAttributes()) {
    		vResult = {};
    		for (nLength; nLength < oXMLParent.attributes.length; nLength++) {
    			iAttrib = oXMLParent.attributes.item(nLength);
    			vResult["@" + iAttrib.nodeName.toLowerCase()] = buildValue(iAttrib.nodeValue.replace(/^\s+|\s+$/g, ""));
    		}
    	}
    	if (oXMLParent.hasChildNodes()) {
    		var iKey, iValue, iXMLChild;
    		for (var iChildId = 0; iChildId < oXMLParent.childNodes.length; iChildId++) {
    			iXMLChild = oXMLParent.childNodes.item(iChildId);
    			if (iXMLChild.nodeType === 1 && !iXMLChild.prefix) {
    				if (nLength === 0) { vResult = {}; }
    				iKey = iXMLChild.nodeName.toLowerCase();
    				iValue = getXMLData(iXMLChild);
    				if (vResult.hasOwnProperty(iKey)) {
    					if (vResult[iKey].constructor !== Array) { vResult[iKey] = [vResult[iKey]]; }
    					vResult[iKey].push(iValue);
    				} else { vResult[iKey] = iValue; nLength++; }
    			} else if (iXMLChild.nodeType === 3) { sTxtContent += iXMLChild.nodeValue.replace(/^\s+|\s+$/g, ""); }
    			else if (iXMLChild.nodeType === 4) { sTxtContent += iXMLChild.nodeValue; }
    		}
    	}
    	if (nLength > 0) { vResult.keyValue = buildValue(sTxtContent); Object.freeze(vResult); }
    	else if (sTxtContent) { vResult = buildValue(sTxtContent); }
    
    	return(vResult);
    }
    
    var xml = getXMLData(doc);
    // hai ottenuto il tuo file! prova a lanciare: alert(JSON.stringify(xml));

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.