Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14
  1. #1

    Problema lettura XML con Javascript da FOREFOX

    Ciao a tutti, spero sia il forum giusto e che qualcuno mi possa aiutare.

    Ho un XML così strutturato:

    codice:
    <TESTI>
        <ITEM0></ITEM0>
        <ITEM1></ITEM1>
        <ITEM2></ITEM2>
        <ITEM3></ITEM3>
        ....ecc
    </TESTI>
    In ogni pagina del mio sito chiamo questa funzione javascript passandogli come "indice" il numero del nodo ITEM che deve leggere dall'xml. ("xml" è naturalmente il nome dell'xml).

    codice:
    function leggiNodi(indice, xml) 
    {
    	if (window.ActiveXObject){
    			alert ("stai usando internet explorer")
    			var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    			xmldoc.async = "false";
    			var languageFolder = getCookie('eltracLanguage');
    			xmldoc.load("text/"+languageFolder+"/"+xml+"");	
    			xmldoc.childNodes[1].childNodes[indice].childNodes[0].text
    			var valoreNodo = xmldoc.childNodes[1].childNodes[indice].childNodes[0].text;
    			document.write (valoreNodo);
    	}else{
    			alert ("non stai usando internet explorer")
                                          //QUI CHE CI DEVO METTERE PER LEGGERE XML DA FIREFOX??
    			
    	}
    }
    Il problema è che con IE tutto funziona bene.. ma con FireFox non va nulla... purtroppo non so proprio come fare a leggere i nodi del mio XML da FIREFOX... Chi può aiutarmi??

    Grazie
    Ho capito che quando poni una domanda e nessuno ti sa rispondere, è arrivato il momento in cui sei tu a poter dare delle risposte agli altri...

  2. #2

    ...........

    Ciao.
    Usa questa:
    Codice PHP:
    function getXmlHttpRequestObject() {
            var 
    receiveReq=false//Clear our fetching variable
            
    try {
                    
    receiveReq = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
            
    } catch (e) {
                    try {
                            
    receiveReq = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
                
    } catch (e) {
                    
    receiveReq false;
                            }
            }
            if (!
    receiveReq && typeof XMLHttpRequest!='undefined') {
                   
    receiveReq = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
            
    }
            return 
    receiveReq;
        } 
    Ad ogni modo in rete di queste funzioni
    ce ne bizzeffe + o - adatte a browser
    datati.




    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Grazei il problema è che non mi ci raccapezzo... come lo applico al mio caso? saresti così gentile da darmi una mano?
    Ho capito che quando poni una domanda e nessuno ti sa rispondere, è arrivato il momento in cui sei tu a poter dare delle risposte agli altri...

  4. #4
    HO PROVATO COSI' MA NON VA...




    codice:
    function leggiNodi(indice, xml) 
    {
    	//if(navigator.userAgent.indexOf("Gecko")!=(-1)){
    	if (window.ActiveXObject){
    			alert ("stai usando internet explorer")
    			var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    			xmldoc.async = "false";
    			var languageFolder = getCookie('eltracLanguage');
    			xmldoc.load("text/"+languageFolder+"/"+xml+"");	
    			xmldoc.childNodes[1].childNodes[indice].childNodes[0].text
    			var valoreNodo = xmldoc.childNodes[1].childNodes[indice].childNodes[0].text;
    			document.write (valoreNodo);
    	}else{
    			alert ("non stai usando internet explorer");
    			var languageFolder = getCookie('eltracLanguage');
    			var xmldoc = new XMLHttpRequest();
    			xmldoc.open("GET","text/"+languageFolder+"/"+xml+"", false);
    			xmldoc.send(null);
    			myXML = xmldoc.responseXML;
    			myXML.childNodes[1].childNodes[indice].childNodes[0].text
    			var valoreNodo = myXML.childNodes[1].childNodes[indice].childNodes[0].text;
    			document.write (valoreNodo);
    	}
    }
    Ho capito che quando poni una domanda e nessuno ti sa rispondere, è arrivato il momento in cui sei tu a poter dare delle risposte agli altri...

  5. #5

    quasi quasi...

    così sembra funzionare... arriva ai nodi infati l'alert ne scrive il nome.. ma non riesco a leggerne i contenuti.. alcuni campi mi risultano #text... che significa? che faccio?


    codice:
    function leggiNodi(indice, xml) {
    	var languageFolder = getCookie('eltracLanguage');
    	if (window.ActiveXObject){
    			alert ("stai usando internet explorer")
    			var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
    			xmldoc.async = "false";
    			xmldoc.load("text/"+languageFolder+"/"+xml+"");	
    			var valoreNodo = xmldoc.childNodes[1].childNodes[indice].childNodes[0].text;
    	}else{
    			alert ("non stai usando internet explorer");
    			var xmldoc = new XMLHttpRequest();
    			xmldoc.open("GET","text/"+languageFolder+"/"+xml, false);
    			xmldoc.send(null);
    			//for (var j in xmldoc){alert (j)}
    			var myXML = xmldoc.responseXML;
    			//for (var j in myXML){document.write (j+"
    ")}
    			alert(myXML.firstChild.childNodes[indice].nodeValue)
    			var valoreNodo = myXML.childNodes[1].childNodes[indice].childNodes[0].text;
    	}
    	document.write (valoreNodo);
    }
    Ho capito che quando poni una domanda e nessuno ti sa rispondere, è arrivato il momento in cui sei tu a poter dare delle risposte agli altri...

  6. #6
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    vado di corsissima, il problema e' che mozilla considera nodi anche gli spazi (anche quelli derivanti dai rimandi a capo)
    qui hai spiegazione e codice di esempio per aggirare il problema
    http://www.w3schools.com/dom/dom_mozilla_vs_ie.asp
    ciao

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da Xinod
    vado di corsissima, il problema e' che mozilla considera nodi anche gli spazi (anche quelli derivanti dai rimandi a capo)
    qui hai spiegazione e codice di esempio per aggirare il problema
    http://www.w3schools.com/dom/dom_mozilla_vs_ie.asp
    ciao
    Ero un secolo che cercavo un link simile: non me lo avete dato ma io leggo anche le domande degli altri , e adesso me lo tengo

    Pietro

  8. #8
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    trovato or ora, Pietro

    FF non ha .text, ma .textContent
    con una funzione del genere, fuori, indipendente da leggiNodi()
    codice:
    function getNode(x){
    	while(x.nodeType!=1){x=x.nextSibling;}
    	return x;
    }
    var valoreNodo = getNode(getNode(myXML.childNodes[1]).childNodes[indice]).textContent
    dovrebbe restituirti quello che cerchi

  9. #9
    Originariamente inviato da Xinod
    trovato or ora, Pietro

    FF non ha .text, ma .textContent
    con una funzione del genere, fuori, indipendente da leggiNodi()
    codice:
    function getNode(x){
    	while(x.nodeType!=1){x=x.nextSibling;}
    	return x;
    }
    var valoreNodo = getNode(getNode(myXML.childNodes[1]).childNodes[indice]).textContent
    dovrebbe restituirti quello che cerchi
    Non va... cioè adesso legge i nodi.. ma li legge a casaccio... non mi rispetta l'indice che gli passo... aiutooooo
    Ho capito che quando poni una domanda e nessuno ti sa rispondere, è arrivato il momento in cui sei tu a poter dare delle risposte agli altri...

  10. #10

    ...........

    Prova questo:
    File xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <TESTI>
    <ITEM id="1">Primo</ITEM>
    <ITEM id="2">Secondo</ITEM>
    <ITEM id="3">Terzo</ITEM>
    <ITEM id="4">Quarto</ITEM>
    <ITEM id="5">Quinto</ITEM>
    </TESTI>
    File Ajax:
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script language="JavaScript" type="text/JavaScript">
    function 
    getXmlHttpRequestObject() {
            var 
    receiveReq=false//Clear our fetching variable
            
    try {
                    
    receiveReq = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
            
    } catch (e) {
                    try {
                            
    receiveReq = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
                
    } catch (e) {
                    
    receiveReq false;
                            }
            }
            if (!
    receiveReq && typeof XMLHttpRequest!='undefined') {
                   
    receiveReq = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
            
    }
            return 
    receiveReq;
        }    
        var 
    receiveReq getXmlHttpRequestObject();        
    function 
    getNewsdoc id
    {
        var 
    itemContent = new Array();
        
    //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
        
    if (receiveReq.readyState == || receiveReq.readyState == 0
        {
            
    //Setup the connection as a GET call to your page
            //True explicity sets the request to asyncronous (default).
            
    receiveReq.open('GET'doc true);
            
    //Set the function that will be called when the XmlHttpRequest objects state changes.
            
    receiveReq.onreadystatechange = function()
            {
                if(
    receiveReq.readyState==4)
                {
                    
    // if status == 200 display text file
                        
    if(receiveReq.status==200)
                    {
                        
    data receiveReq.responseXML.getElementsByTagName('ITEM');  
                        for(var 
    i=0;i<data.length;i++)
                        {
                            
    itemContent[parseInt(data[i].getAttribute('id'))] = data[i].firstChild.nodeValue;
                        } 
                        
    alert(itemContent[id]);
                   }
            }
            }
            
    //Make the actual request.
            
    receiveReq.send(null);    
        }    
        
    }
    window.onload = function()
    {
        
    getNews("item.xml",1);

    }
    </script>

    </head>

    <body>
    </body>
    </html> 




    PS.
    Non fare caso ai commenti just recycled
    Volendo a getNews puoi passare anche altri parametri.
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

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.