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

    [DOM] Accedere al contenuto di un nodo

    Data la seguente stringa XML

    codice:
    <XMLresponse>
    	<ticket date="24.12.2007">
    		<timestamp>12918723498</timestamp>
    		<text>Testo</text>
    	</ticket>
    </XMLresponse>
    opportunamente parsata come documento XML, come si accede al contenuto di timestamp?

    Codice PHP:

    // funziona:
    var ticket XMLdoc.getElementsByTagName('ticket');
    alert(msg[0].getAttribute('date')); // restituisce la data

    // non funziona:
    var timestamp XMLdoc.childNodes[0].childNodes[0].nodeValue;
    alert(timestamp); // restituisce "null"

    // anche questa restituisce null:
    var timestamp XMLdoc.getElementsByTagName('timestamp');
    alert(timestamp[0].nodeValue); 
    Emanuele DG
    <?php echo "Proverbio zen(d): vivi ogni giorno come se fosse il ".date('d M Y', time()); ?>
    Intellectual property

  2. #2
    Uppo con il codice:

    Codice PHP:
    <html>
    <
    head>
    <
    title>XML Parsing</title>
    <
    script type="text/javascript">
    <!--
    function 
    XMLparse() {
        var 
    XMLresponse '<XMLresponse><ticket date="24.12.2007"><timestamp>12918723498</timestamp><text>Testo</text></ticket></XMLresponse>';
        
        if (
    window.ActiveXObject) {
            
    // MSIE:
            
    var doc = new ActiveXObject('Microsoft.XMLDOM');
            
    doc.async 'false';
            
    doc.loadXML(XMLresponse);
        } else {
            
    // W3C compliant:
            
    var parser = new DOMParser();
            var 
    doc parser.parseFromString(XMLresponse'text/xml');
        }
        var 
    XMLdoc doc.documentElement;
        var 
    ticket XMLdoc.getElementsByTagName('ticket');
        
    alert(ticket[0].getAttribute('date'));
        var 
    timestamp XMLdoc.getElementsByTagName('timestamp');
        
    alert(timestamp[0].nodeValue);
    }
    //-->
    </script>
    </head>

    <body>
    [url="#"]test[/url]
    </body>
    </html> 
    Emanuele DG
    <?php echo "Proverbio zen(d): vivi ogni giorno come se fosse il ".date('d M Y', time()); ?>
    Intellectual property

  3. #3
    function XMLparse() {
    var XMLresponse = '<XMLresponse><ticket date="24.12.2007"><timestamp>12918723498</timestamp><text>Testo</text></ticket></XMLresponse>';

    if (window.ActiveXObject) {
    // MSIE:
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.async = 'false';
    doc.loadXML(XMLresponse);
    } else {
    // W3C compliant:
    var parser = new DOMParser();
    var doc = parser.parseFromString(XMLresponse, 'text/xml');
    }
    var XMLdoc = doc.documentElement;
    var ticket = XMLdoc.getElementsByTagName('ticket');
    alert(ticket.item(0).getAttribute('date'));
    var timestamp = XMLdoc.getElementsByTagName('timestamp');
    alert(timestamp.item(0).firstChild.nodeValue);
    }


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

  4. #4
    Originariamente inviato da whisher
    function XMLparse() {
    var XMLresponse = '<XMLresponse><ticket date="24.12.2007"><timestamp>12918723498</timestamp><text>Testo</text></ticket></XMLresponse>';

    if (window.ActiveXObject) {
    // MSIE:
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.async = 'false';
    doc.loadXML(XMLresponse);
    } else {
    // W3C compliant:
    var parser = new DOMParser();
    var doc = parser.parseFromString(XMLresponse, 'text/xml');
    }
    var XMLdoc = doc.documentElement;
    var ticket = XMLdoc.getElementsByTagName('ticket');
    alert(ticket.item(0).getAttribute('date'));
    var timestamp = XMLdoc.getElementsByTagName('timestamp');
    alert(timestamp.item(0).firstChild.nodeValue);
    }



    Grazie, così è perfetto

    Evinco che per leggere le collezioni di oggetti si usa la proprietà item(nodeIndex) invece dell'indice dell'array.

    Una precisazione sul DOM di JavaScript:
    perché nella riga di codice
    codice:
    timestamp.item(0).firstChild.nodeValue;
    per prendere il contenuto del tag "timestamp" invochi la proprietà nodeValue sul suo nodo figlio (firstChild) ?

    Per caso JavaScript considera il suo contenuto come un textNode che è figlio di "timestamp"?
    Mi sembra concettualmente strano... Dico questo perché nel DOM di PHP, quando hai l'oggetto "timestamp", ne prelevi il contenuto semplicemente con il metodo get_content() (che corrisponde a nodeValue).
    Emanuele DG
    <?php echo "Proverbio zen(d): vivi ogni giorno come se fosse il ".date('d M Y', time()); ?>
    Intellectual property

  5. #5
    Per caso JavaScript considera il suo contenuto come un textNode che è figlio di "timestamp"
    è proprio così dai un occhio qui


    Dico questo perché nel DOM di PHP, quando hai l'oggetto "timestamp", ne prelevi il contenuto semplicemente con il metodo get_content() (che corrisponde a nodeValue).
    ho cominciato ad usare il dom in php solamente
    con le dom functions quindi non saprei ........
    usando queste ultime il comportamento è lo stesso.

    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 © 2026 vBulletin Solutions, Inc. All rights reserved.