Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427

    paginazione con dom e xhtml

    grazie ad un tutorial di pro.html
    sto provando ad impaginare un file xml.



    ma ho un errore di lettura del file js.

    il file per la lettura è:

    codice:
    <html>
    <head>
    <title>Estrarre dati da un file XML con JavaScript</title>
    <style type="text/css">
    h1 { color: #800000; font-size: 15px; font-family: Verdana; }
    td { background-color: #FFFFFF; font-size: 12px; font-family: Verdana; }
    </style>
    <script language="javascript" for="window" event="onload" src="leggi.js"></script>
    </head>
    <body>
    
    <xml id="DSO_risultati" src="risultati_now.xml"></xml>
    
    <h1 align="center">risultati</h1>
    
    <div id="Stampa"></div>
    
    </body>
    </html>

    il file js è :

    codice:
    var Mostra = ""; var StringaXml = DSO_risultati.XMLDocument;
    var i;
    for (i=0; i<StringaXml.documentElement.childNodes.length; i++) {
     Mostra += "<table bgcolor='#F9F9F9' align='center' width='450' border='1' bordercolor='#CCCCCC'>"
     Mostra += "<tr>"
     Mostra += "<td colspan='2' style='background-color: #008080;'>"
     Mostra += "<font color='#FFFFFF'>Amico # " + (i + 1) + "</font>"
     Mostra += "</td>"
     Mostra += "</tr>"
     Mostra += "<tr>"
     Mostra += "<td width='100'>Nome:</td>"
     Mostra += "<td>" + StringaXml.documentElement.childNodes(i).childNodes(0).text + "</td>"
     Mostra += "</tr>"
     Mostra += "<tr>"
     Mostra += "<td>Cognome:</td>"
     Mostra += "<td>" + StringaXml.documentElement.childNodes(i).childNodes(1).text + "</td>"
     Mostra += "</tr>"
     Mostra += "<tr>"
     Mostra += "<td>Telefono:</td>"
     Mostra += "<td>" + StringaXml.documentElement.childNodes(i).childNodes(2).text + "</td>"
     Mostra += "</tr>"
     Mostra += "<tr>"
     Mostra += "<td>Indirizzo:</td>"
     Mostra += "<td>" + StringaXml.documentElement.childNodes(i).childNodes(3).text + "</td>"
     Mostra += "</tr>"
     Mostra += "<tr>"
     Mostra += "<td>Città:</td>"
     Mostra += "<td>" + StringaXml.documentElement.childNodes(i).childNodes(4).text + "</td>"
     Mostra += "</tr>"
     Mostra += "</table>"
     Mostra += "
    ";
    }
    Stampa.innerHTML = Mostra;

    l'errore è:
    Errore: DSO_risultati is not defined
    File sorgente: mio_sito/leggi.js
    Riga: 1

  2. #2
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    up...

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    l' errore lo hai con ffox, giusto?
    perche' il metodo li' impiegato funziona solo con IE

    intanto ti posto una versione cross-browser (chiaramente x i browser che permettono la lettura di un file xml via javascript)
    codice:
    var xmlDoc
    function loadXML(fileName){
    	// IE
    	if (window.ActiveXObject){
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.async=false;
    		xmlDoc.load(fileName);
    		getmessage();
    	}
    	// Mozilla & Co.
    	else if (document.implementation && document.implementation.createDocument){
    		xmlDoc= document.implementation.createDocument("","",null);
    		xmlDoc.load(fileName);
    		xmlDoc.onload=getmessage
    	}
    	// altri browser
    	else {
    		alert('il tuo browser non permette il parsing di file .xml');
    	}
    }
    window.onload=function(){loadXML("rubrica.xml");}
    a questo punto si aprono 2 strade:
    la prima, semplice
    dice di rilevare i valori come valore del nodo del primo figlio dell' elemento con indice n di document.getElementsByTagName('tag')
    che non e' altro che il testo contenuto in <tag></tag>

    x capirci:
    codice:
    alert(xmlDoc.getElementsByTagName('rubrica')[0].getElementsByTagName('cognome')[1].childNodes[0].nodeValue);
    restituisce "Rossi"


    l' altra, molto piu' complicata ma l' unica seguibile nel caso non si conoscano i nomi dei tag nell' xml o l' esatta alberatura, e' costruirsi (o trovarne uno pronto) un treewalker che parsi l' alberatura fino a giungere ai valori desiderati, tenendo conto di come il w3c valuta il whitespace (i rimandi a capo sono un nodo)

    ci sono alcune fonti online che offrono alcuni importanti spunti di studio, x es.
    http://www.quirksmode.org/dom/importxml.html

    ciao

  4. #4
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    grazie per avere risposto...

    l'alberatura la conosco...

    provo ad usare la prima..

    a presto.

  5. #5
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427


    ho fatto cosi...:

    <script type="text/javascript">
    var xmlDoc
    function loadXML(fileName){
    // IE
    if (window.ActiveXObject){
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(fileName);
    getmessage();
    }
    // Mozilla & Co.
    else if (document.implementation && document.implementation.createDocument){
    xmlDoc= document.implementation.createDocument("","",null) ;
    xmlDoc.load(fileName);
    xmlDoc.onload=getmessage
    }
    // altri browser
    else {
    alert('il tuo browser non permette il parsing di file .xml');
    }
    }
    window.onload=function(){loadXML("rubrica.xml");}

    var nome = xmlDoc.getElementsByTagName("LIBRO/TITOLO");
    var cognome = xmlDoc.getElementsByTagName("LIBRO/AUTORE");

    var i = 0;
    for (i=0; i<nome.length; i++)
    {
    document.write(nome[i].text + " " + cognome[i].text + "
    ");
    }

    xmlDoc.Close();

    </script>

    </body>
    </html>

    ma ho due errori :
    Errore: xmlDoc has no properties
    File sorgente: /rubrica_test.htm
    Riga: 30

    Errore: getmessage is not defined
    File sorgente: /rubrica_test.htm
    Riga: 21



  6. #6
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    Originariamente inviato da agenti
    <script type="text/javascript">
    var xmlDoc
    function loadXML(fileName){
    // IE
    if (window.ActiveXObject){
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(fileName);
    getmessage();
    }
    // Mozilla & Co.
    else if (document.implementation && document.implementation.createDocument){
    xmlDoc= document.implementation.createDocument("","",null) ;
    xmlDoc.load(fileName);
    xmlDoc.onload=getmessage
    }
    // altri browser
    else {
    alert('il tuo browser non permette il parsing di file .xml');
    }
    }
    window.onload=function(){loadXML("rubrica.xml");}

    function getmessage(){
    var nome = xmlDoc.getElementsByTagName("LIBRO/TITOLO");
    var cognome = xmlDoc.getElementsByTagName("LIBRO/AUTORE");

    var str = '';
    for (var i=0; i<nome.length; i++){
    str+=nome[i].childNodes[0].nodeValue + " " + cognome[i].childNodes[0].nodeValue + "
    ";
    }
    document.getElementById('stampa').innerHTML=str;
    }
    </script>
    <div id="stampa"></div>
    </body>
    </html>
    ti ho corretto quotando, riprova

    valuta anche se LIBRO/TITOLO con quello slash in mezzo sia un nome valido

  7. #7
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    rasentiamo l'assurdo...

    adesso non va più in eerrore ma non stampa nulla...
    ho provato anche senza LIBRO/

    questo è l'xml:

    <?xml version="1.0" ?>
    - <LIBRO>
    <AUTORE>Wilbur Smith</AUTORE>
    <TITOLO>Come il mare</TITOLO>
    <COPERTINA>Tascabile economico</COPERTINA>
    <PAGINE>300</PAGINE>
    <PREZZO>Euro 10,00</PREZZO>
    </LIBRO>

    ciao...

  8. #8
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    credo sia solo un errore di copia
    ma quel - prima di <LIBRO> non ci sta bene
    non e' un xml valido cosi'

    corretto quello, salvando questo
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title></title>
        <script type="text/javascript">
        var xmlDoc
        function loadXML(fileName){
        // IE
        if (window.ActiveXObject){
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.load(fileName);
        getmessage();
        }
        // Mozilla & Co.
        else if (document.implementation && document.implementation.createDocument){
        xmlDoc= document.implementation.createDocument("","",null);
        xmlDoc.load(fileName);
        xmlDoc.onload=getmessage
        }
        // altri browser
        else {
        alert('il tuo browser non permette il parsing di file .xml');
        }
        }
        window.onload=function(){loadXML("libri.xml");}
    
        function getmessage(){
        var nome = xmlDoc.getElementsByTagName("TITOLO");
        var cognome = xmlDoc.getElementsByTagName("AUTORE");
    
        var str = '';
        for (var i=0; i<nome.length; i++){
        str+=nome[i].childNodes[0].nodeValue + " " + cognome[i].childNodes[0].nodeValue + "
    ";
        }
        document.getElementById('stampa').innerHTML=str;
        }
        </script>
    </head>
    <body>
    <div id="stampa"></div>
    </body>
    </html>
    l' output prodotto da IE e ffox e' identico ed e':
    Come il mare Wilbur Smith

    ciao

  9. #9
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    si era un problema dell'xml,
    grazie...

    ma in realtà il file non lo devo caricare da locale ma lo ottengo dietro risposta di un server remoto...

    tramite una funzione di richiesta:

    <script>
    var route = "E1T5Z8GLJAEOBK9V"
    var results = "<CommandList>"+
    "\n <CheckRouting>"+loginid+
    "\n <RoutingId>"+route+
    // "\n <Filter>"+
    // "\n <FirstResult>0</FirstResult>"+
    // "\n <NumberOfResults>30</NumberOfResults>"+
    // "\n </Filter>"+
    // "\n <SortList>"+
    // "\n <Sort>"+
    // "\n <Order>forward</Order>"+
    // "\n <Type>price</Type>"+
    // "\n </Sort>"+
    // "\n </SortList>"+
    "\n </CheckRouting>"+
    "\n</CommandList>"+
    "\n";

    function getXMLHttp_ok()
    {
    var xmlhttp1 = null;
    if (window.ActiveXObject)
    {
    if (navigator.userAgent.toLowerCase().indexOf("msie 5") != -1)
    {
    xmlhttp1 = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    xmlhttp1 = new ActiveXObject("Msxml2.XMLHTTP");
    }

    }
    if (!xmlhttp1 && typeof(XMLHttpRequest) != 'undefined') {
    xmlhttp1 = new XMLHttpRequest()
    }
    return xmlhttp1


    objHTTP1 = getXMLHttp_ok();
    objHTTP1.open("post", "http://demo.miosito.com/XML", true);
    objHTTP1.send(results)
    alert(objHTTP1.responseXML);
    }

    </script>

    come posso adattarlo all'impaginazione precedente?
    devo creare sempre uan funzione di lettura?

    ciao e grazie.



    carico il file xml...
    coe posso adattarlo

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.