Generate xml automatically

Asked

Viewed 626 times

0

I want after reading the html page with the file_get_contents and select what matters with the preg_match_all a is automatically generated xml (calling the function below) and generating the file.

As there are several lists to be generated in the same php, I didn’t want to have to repeat the commands of XmlWriter, just call the function to do automatically.

<?php

header ('Content-Type:application/xml');
$html = file_get_contents('http://www.teste.com/');
$re = '/href=\'(.*?)\'>(.*?)</(.*?)';
preg_match_all($re, $html, $key);
foreach($key[1] as $i)  

function lista_xml($xml){   
    $xml = new XMLWriter();
    $xml->openMemory();
    $xml->startDocument('1.0','UTF-8');
    $xml->startElement('items');
    $xml->startElement('lista');
    $xml->writeCData('$nome');
    $xml->endElement();
    $xml->startElement('canal');
    $xml->startElement('titulo');
    $xml->writeCData('.$key[2][$i].');
    $xml->endElement();
    $xml->startElement('img');
    $xml->writeCData();
    $xml->endElement();
    $xml->startElement('link');
    $xml->writeCData('.$key[1][$i].');
    $xml->endElement();
    $xml->startElement('detalhes');
    $xml->writeCData('<center><img src='".$key[3][$i]."'/> '.$key[2][$i].'</center>');
    $xml->endElement();
    $xml->endDocument();
    echo $xml ->outputMemory();
    $xml->flush();
    unset($xml);
    }
?>

1 answer

1


First that your foreach is doing nothing and second that the lista_xml is never called, your code does nothing simply, now the problems grave, single quotes do not interpret variables:

$xml->writeCData('$nome');

This will try to generate a tag literally named $nome and not with the value.

You quote like you’re concatenating, but it doesn’t make sense:

$xml->writeCData('.$key[1][$i].');

This will write a tag like this:

<foo><!CDATA[[$key[1][$i]]]></foo>

Instead of the content will literally write <!CDATA[[$key[1][$i]]]>

Your regex is also wrong:

 $re = '/href=\'(.*?)\'>(.*?)</(.*?)';

In PCRE there must always be delimiters https://secure.php.net/manual/en/regexp.reference.delimiters.php, in case it is possible that it was a typo and you forgot the / at the end and escape the / after < (would be better to use another delimiter), revised and removed which is unnecessary:

 $re = '#href=[\'"](.*?)[\'"]>(.*?)</(.*?)#';

note: I traded it for ['"] because it will take links that have href="..." and href='...'

did not want to have to repeat the commands of Xmlwriter, just call the function to do automatically.

Yes, just create a file and function, it should look like this:

gerarxml.php

<?php

function GerarXml($nome, $re, $html)
{
    header ('Content-Type:application/xml');
    preg_match_all($re, $html, $data);

    //Inicia o XML
    $xml = new XMLWriter();
    $xml->openMemory();
    $xml->startDocument('1.0','UTF-8');
    $xml->startElement('items');

    foreach ($data[1] as $key => $value) {

        $xml->startElement('lista');
        $xml->writeCData($nome);
        $xml->endElement();

        $xml->startElement('canal');

        $xml->startElement('titulo');
        $xml->writeCData($data[2][$key]);
        $xml->endElement();

        $xml->startElement('img');
        $xml->writeCData('algo aqui');
        $xml->endElement();

        $xml->startElement('link');
        $xml->writeCData($data[1][$key]);
        $xml->endElement();

        $xml->startElement('detalhes');
        $xml->writeCData('<center><img src="'.$data[3][$key].'"/> '.$data[2][$key].'</center>');
        $xml->endElement();

        $xml->endElement(); //Finaliza canal
    }

    //Finaliza o </items>
    $xml->endElement();

    $xml->endDocument();
    echo $xml->outputMemory();
    $xml->flush();
}

To execute call so:

<?php

require_once 'gerarxml.php';

GerarXml('teste', '#href=[\'"](.*?)[\'"]>(.*?)</(.*?)#', file_get_contents('http://seusite.com/'));
  • William, thank you for your help. I tested and presented an error on line 33. Naturally due to the quotation marks. No double quotes "error on line 2 at column 1: Extra content at the end of the Document".

  • I tested it several times but to no avail. I used a direct html text without using file_get_contents and the result is still an empty page. I didn’t want to have to use an external class for XML. E.

  • Yes, Guilherme. I tested on 4 different servers. On a Mac (localhost) PHP 7.1, on a NAS (PHP 5.4), on a free server and in xamp (Windows). All with display_errors on. I must be doing something wrong, certainly. The error message: (This page contains the following errors: error on line 2 at column 1: Extra content at the end of the Document)

  • @Antoniooliveira fixed the script, copy again, if the same error occurs remove this line header ('Content-Type:application/xml'); and save the script, then press F5 in the browser will appear all errors.

  • Hi William. Now it worked. Thank you so much for your help.

  • @Antoniooliveira will then mark the answer correctly, please, if you do not know how to do it read this: https://pt.meta.stackoverflow.com/q/1078/3635

  • Only one detail was missing: What would the code look like if the xml generating part was a function (from "/Start XML"), to avoid having to repeat all the cell inclusion commands? In a news site, for example, it would have to generate 7 xml tables, which would make the code very long.

  • @Antoniooliveira but you want to generate 7 different xmls in the same request? I believe this is not possible

  • No. Requests will be different. The first xml will forward to a second page and so on. It will be a different regex and naturally another request.

  • Hi William. The job was perfect. Just what I needed. Thank you.

  • The function was perfect. But I would like to ask another question. How do I store in memory 'titulo' 'img' and 'link' ? So that in the second table you can use: GerarXml('titulo', '#o regex da página secundária#', file_get_contents('link')); ?? how these elements are dynamically generated in the function you created, with the foreach ($data[1] as $key => $value), in the second request this data also need to be variable.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.