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

    XML e Javascript, errore nell'estrarre la root

    Salve a tutti,
    Ho il seguente codice che ha il compito di estrarre da un file xml la lista degl online e stamparla all'interno di un div.
    Codice PHP:
    var online = new Array();

    var 
    XMLFile "/online.xml"
    var xml null;
    function 
    getXML(){
        if (
    document.implementation && document.implementation.createDocument)
            {
            
    xml document.implementation.createDocument("","",null)
            
    xml.onload=Leggi
            xml
    .load(XMLFile);
            }
            else if (
    window.ActiveXObject)
                {
                
    xml = new ActiveXObject("Microsoft.XMLDOM")
                
    xml.onreadystatechange IEGo;
                
    xml.load(XMLFile);
                }
    }
        
    function 
    IEGo()
        {
        if (
    xml.readyState == 4)
            
    Leggi();
        }    
        
    function 
    Leggi()
        {
        var 
    Nodo xml.getElementById("online").getElementsByTagName("pg");
        
    LeggiPG(Nodo);

        var 
    testo "ONLINE
    "
    ;
        for (var 
    online.length i++ ){
            if(
    online[i].sesso "M"){
            
    testo += "<a href=\"scheda_on.php?pg_id="+online[i].id+"\" style=\"color:blue;\"\">"+
                    
    online[i].nome_cognome+"</a></span>
    "
    ;
            } else {
            
    testo += "<a href=\"scheda_on.php?pg_id="+online[i].id+"\" style=\"color:pink;\"\">"+
                    
    online[i].nome_cognome+"</a></span>
    "
    ;
            }
        }
        
    document.getElementById("blabla").innerHTML=testo;
        }

    function 
    LeggiPG(obj){
        for (
    obj.length i++)
            
    online[i] = new dati(
                               
    getValue(obj[i],"nome_cognome") , 
                            
    getValue(obj[i],"id") , 
                            
    getValue(obj[i],"sesso")
                            ) 
    }
    function 
    getValue(vobj,tag,param){
        if (
    param)
            return 
    vobj.getElementsByTagName(tag)[0].getAttribute(param);
        else
            return 
    vobj.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
    }

    function 
    dati(nome_cognomeidsesso){
        
    this.nome_cognome=nome_cognome;
        
    this.id=id;
        
    this.sesso=sesso;

    Il problema è che ritorna un errore, più precisamente su :
    xml.getElementById("online") has no properties
    [Break on this error] var Nodo = xml.getElementById("online").getElementsByTagName( "pg");

    Il file xml è
    Codice PHP:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <online>
        <pg>
            <nome_cognome>
                utente prova
            </nome_cognome>
            <id>
                2
            </id>
            <sesso>
                M
            </sesso>
        </pg>
    </online>
    Da cosa può dipendere?

  2. #2

    responseXML non ha proprietà??

    salve a tutti,
    ho questo codice:
    Codice PHP:
    function assegnaXMLHttpRequest() {

        var 
    xmlreq false;

        if (
    window.XMLHttpRequest) {

            
    // Create XMLHttpRequest object in non-Microsoft browsers
            
    xmlreq = new XMLHttpRequest();

        } else if (
    window.ActiveXObject) {

            
    // Create XMLHttpRequest via MS ActiveX
            
    try {
                
    // Try to create XMLHttpRequest in later versions
                // of Internet Explorer

                
    xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

            } catch (
    e1) {

                
    // Failed to create required ActiveXObject

                
    try {
                    
    // Try version supported by older versions
                    // of Internet Explorer

                    
    xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

                } catch (
    e2) {

                    
    // Unable to create an XMLHttpRequest with ActiveX
                
    }
            }
        }

        return 
    xmlreq;

    e la sua relativa chiamata:
    Codice PHP:
    function caricaTesto() {
        var
            
    ajax assegnaXMLHttpRequest(),
            
    elemento prendiElementoDaId("etr"),
            
    usaLink true;  
        if(
    ajax) {
            
    usaLink false;
            
    ajax.open("get""ajax/online.php"true);
            
    ajax.setRequestHeader("connection""close");
            
    ajax.onreadystatechange = function() {
                if(
    ajax.readyState === readyState.COMPLETATO) {
                    if(
    statusText[ajax.status] === "OK"){
                        var 
    XMLDoc ajax.responseXML;
                        
    alert(XMLDoc);
                        var 
    XMLroot XMLDoc.getElementsByTagName("online");
                    }
                } 
            }
            
    ajax.send(null);
        }
        return 
    usaLink;

    Come mai restituisce quest'errore:
    ajax.responseXML has no properties
    [Break on this error] var XMLDoc = ajax.responseXML.documentElement;
    tenendo conto che se utilizzo responseText, mi restituisce il file xml corretto, usando responseXML restituisce null
    ?

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.