Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    28

    Analizzare file XML con PHP

    Ciao a tutti.
    Devo analizzare un file XML con uno script PHP.
    Ho letto alcune guide, ma ho molta confusione in testa, perchè vedo che ci sono un sacco di modi per farlo.
    Un esempio di file che devo analizzare è questo:
    codice:
    <nitf>
        <head>
          <title>
            
            Titolo del file XML
          </title>
          <meta name ="author" content ="CNZ" />
          
          <meta name ="writer" />
          
          <meta name ="priority" content ="U" />
          <docdata>
            
            <doc-id id-string ="WJO20915" />
            
            <date.issue norm ="2007-12-18 14:08" />
            
            <date.release norm ="2007-12-18 14:08" />
          </docdata>
        </head>
        <body>
          <body.head>
            <hedline>
              <hl1>
                
                Titolo
              </hl1>
            </hedline>
            <byline>
              
              Sottotitolo
            </byline>
            <distributor>
              <org>
                
                Agenzia
              </org>
            </distributor>
            <dateline>
              <location>
                
                ROMA
              </location>
            </dateline>
          </body.head>
          <body.content>
            
            <lang lang ="it" />
            <block>
              
              <img source ="7e4bf043c5059fda66b177daea8532f9.jpg" />
              
    
    
                
                Testo dell'articolo
              </p>
            </block>
          </body.content>
        </body>
      </nitf>
    In pratica, devo arrivare ad avere tutti i valori dei vari tag del file XML in diverse variabili nello script PHP, o al limite avere il tutto raggruppato in un array.

    Come posso fare?
    Grazie mille per l'aiuto!
    Ciao.

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

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    28
    Grazie.
    Ho dato una veloce scorsa all'articolo e sembra interessante, ma non ho trovato alcuna informazione circa l'interpretazione delle righe <meta>, come ad esempio

    codice:
    <meta name ="author" content ="CNZ" />
    O sbaglio?

  4. #4
    Originariamente inviato da decibel
    Grazie.
    Ho dato una veloce scorsa all'articolo e sembra interessante, ma non ho trovato alcuna informazione circa l'interpretazione delle righe <meta>, come ad esempio

    codice:
    <meta name ="author" content ="CNZ" />
    O sbaglio?
    E' un nodo come un'altro.

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

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    28
    Originariamente inviato da whisher
    E' un nodo come un'altro.
    Ossia? Come lo recupero?
    Puoi aiutarmi per favore? Grazie!

  6. #6
    Originariamente inviato da decibel
    Ossia? Come lo recupero?
    Puoi aiutarmi per favore? Grazie!


    Codice PHP:
    $doc= new DOMDocument();
    $doc->load('xml.xml');
    $nodeListMeta$doc->getElementsByTagName('meta');
    /*
    Con $nodeListMeta->item(0) accedi al primo 
    Con $nodeListMeta->item(1) accedi al secondo
    */
    var_dump($nodeListMeta->item(0)->getAttribute('name'));
    var_dump($nodeListMeta->item(0)->getAttribute('content')); 
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  7. #7
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    28
    Grazie mille! Sei molto gentile!
    Ma non c'è un modo per dire di recuperare la stringa in base al nome, invece che in base al numero d'ordine della stringa?
    Se il file XML mi arriva con le stringhe invertite di posto, ad esempio, così può essere un problema..
    Hai un'idea?

  8. #8
    Originariamente inviato da decibel
    Grazie mille! Sei molto gentile!
    Ma non c'è un modo per dire di recuperare la stringa in base al nome, invece che in base al numero d'ordine della stringa?
    Se il file XML mi arriva con le stringhe invertite di posto, ad esempio, così può essere un problema..
    Hai un'idea?
    Codice PHP:
    $doc= new DOMDocument();
    $doc->load('xml.xml');
    $nodeListMeta$doc->getElementsByTagName('meta');
    $metaAsArray= array();
    foreach(
    $nodeListMeta as $node){
        
    $metaAsArray[$node->getAttribute('name')]= $node->getAttribute('content');
    }
    var_dump($metaAsArray); 
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    28
    Grazie mille ancora! Funziona :-)
    Ora però non riesco ad accere a i tag "non chiusi", come:

    codice:
    <docdata>
      
      <doc-id id-string ="WJO20915" />
      
      <date.issue norm ="2007-12-18 14:08" />
      
      <date.release norm ="2007-12-18 14:08" />
    </docdata>
    
    <img source ="7e4bf043c5059fda66b177daea8532f9.jpg" />
    <lang lang ="it" />
    Ho notato che un semplice
    Codice PHP:
    $doc->getElementsByTagName("img"); 
    non basta...

  10. #10
    Originariamente inviato da decibel
    Grazie mille ancora! Funziona :-)
    Ora però non riesco ad accere a i tag "non chiusi", come:

    codice:
    <docdata>
      
      <doc-id id-string ="WJO20915" />
      
      <date.issue norm ="2007-12-18 14:08" />
      
      <date.release norm ="2007-12-18 14:08" />
    </docdata>
    
    <img source ="7e4bf043c5059fda66b177daea8532f9.jpg" />
    <lang lang ="it" />
    Ho notato che un semplice
    Codice PHP:
    $doc->getElementsByTagName("img"); 
    non basta...
    Non vedo dove sia il problema:

    Codice PHP:
    $doc= new DOMDocument();
    $doc->load('xml.xml');
    $nodeListImg$doc->getElementsByTagName('img');
    var_dump($nodeListImg->item(0)->getAttribute('source')); 

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