Ecriture .xml (en php)

Bonjour a tous,
J'ai un fichier .txt dans lequel il y a plusieurs millier de ligne (chaque ligne une urls)
et j'aimerai créer un .xml sitemap (en utilisant le php) avec ce genre d'ossature :

<?xml version="1.0" encoding="UTF-8" ?>
- <urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.sitemaps.org/schemas/sitemap/0.9/ https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
- <url>
<loc>url1</loc>
</url>
- <url>
<loc>url2</loc>
</url>
...
- <url>
<loc>urln</loc>
</url>
</urlset>

Quelqu'un pourrait-il m'aider car je ne connais la methode pour ecrire un .xml en recuperant les lignes de mon .txt
Configuration: Windows XP
Firefox 2.0.0.20

12 réponses

  1. Pour l'instant la taille du fichier n'est pas importante, j'ai donc essayer avec un fichier moins volumineux.
    Mais en fait je voudrais faire en sorte d'avoir un fichiers .xml en sortie.
    Je ne voit pas du tout comment faire...

    help please...
    1. Est-ce que tu mets bien le "M" après le chiffre ?
      1. Non je ne l'ai pas oublier... :s
      2. @avion-f16Ben en fait, pour les gros fichiers .txt, j'ai toujours un problème (memory_limit)
        Mais sinon ta méthode fonctionne parfaitement, juste il me faudrait rajouter ces ligne en debut de .xml :

        <?xml version="1.0" encoding="UTF-8" ?>
        <urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.sitemaps.org/schemas/sitemap/0.9/ https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    2. J'ai essayer 64, 256, 512 et même 1024...
      Sa change même pas l'erreur c'est toujours : "...Allowed memory size of 134217728 bytes exhausted (tried to allocate 117682198..."
      1. J'ai beau changer la valeur de cette ligne dans le php.ini et rien ... Toujours la même erreur :( :
        "<b>Fatal error</b>: Allowed memory size of 134217728 bytes exhausted (tried to allocate 117682198 bytes) in <b>W:\var\www\ecriture xml\index.php</b> on line <b>7</b><br />"
        1. Je travaille en local =)

          Est-ce cette ligne qu'il faudrait que je modifie dans le php.ini :
          " memory_limit = 128M; " ou pas?
          1. C'est le fichier qui dépasse la taille de traitement autorisée dans le php.ini
            Regarde dans la doc ou la faq de ton hébergeur ou contactes-les.
            1. J'ai cette erreur lors de l'execution :
              <b>Fatal error</b>: Allowed memory size of 134217728 bytes exhausted (tried to allocate 117682198 bytes) in <b>W:\var\www\ecriture xml\index.php</b> on line <b>7</b><br />

              line 7 : "$lignes = explode("\n", file_get_contents('sitemap.txt'));"

              Est-ce un pb avec la fonction file_get_contents ou un fichier .txt trop volumineux (113 Mo) ?
              1. <?php
                header('Content-type: text/xml; charset=UTF-8');
                
                $dom = new DOMDocument('1.0','utf-8');
                $root = $dom->createElement('root');
                
                $lignes = explode("\n", file_get_contents('fichier.txt'));
                foreach($lignes as $url) {
                	if(!empty($url)) {
                		$urlNode = $dom->createElement('url');
                		$locNode = $dom->createElement('loc',$url);
                		$urlNode->appendChild($locNode);
                		$root->appendChild($urlNode);
                	}
                }
                
                $dom->appendChild($root);
                echo $dom->saveXML();
                ?>
                1. @biohazard-34
                  <?php
                  header('Content-type: text/xml; charset=UTF-8');
                  
                  $dom = new DOMDocument('1.0','utf-8');
                  $root = $dom->createElement('urlset');
                  $root->setAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
                  $root->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
                  $root->setAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
                  
                  $lignes = explode("\n", file_get_contents('fichier.txt'));
                  foreach($lignes as $url) {
                  	if(!empty($url)) {
                  		$urlNode = $dom->createElement('url');
                  		$locNode = $dom->createElement('loc',$url);
                  		$urlNode->appendChild($locNode);
                  		$root->appendChild($urlNode);
                  	}
                  }
                  
                  $dom->appendChild($root);
                  echo $dom->saveXML();
                  ?>