Generate xml automatically

0

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

As there are several lists to be generated in the same php, I did not 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);
    }
?>
    
asked by anonymous 25.09.2017 / 20:36

1 answer

1

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

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

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

You have opened quotation marks as if you were concatenating, but it does not make sense:

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

This will write a tag like this:

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

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

Your regex is also wrong:

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

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

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

Note: I changed it to ['"] because it will get links that have href="..." and href='...'

     

I did not want to have to repeat the XmlWriter commands, just call the function to do it automatically.

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

generatexml.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 run call like this:

<?php

require_once 'gerarxml.php';

GerarXml('teste', '#href=[\'"](.*?)[\'"]>(.*?)</(.*?)#', file_get_contents('http://seusite.com/'));
    
25.09.2017 / 20:48