Insert data in specific part from a file

Asked

Viewed 30 times

0

I’m using the base feed file from google, and need to change the information dynamically to generate an xml file, I am using this approach, with file_puts_contents(), to put the tags <item> in the archive:

file_put_contents('feed.xml', $xml, FILE_APPEND);

only that the end of the xml file has to end with this structure:

</channel>
</rss>

and FILE_APPEND is putting below it, is there any way to insert the content in a specific location or always add the </channel></rss> at the end of the loop?

EDIT: Xml standard google:

    <?xml version="1.0"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
    <channel>
        <title>O nome do feed de dados</title>
        <link>http://www.example.com</link>
        <description>Uma descrição do conteúdo</description>

        <item>
            <title>Suéter de lã vermelho</title>
            <link> http://www.example.com/item1-info-page.html</link>
            <description>Confortável e macio, este suéter manterá você aquecido nas noites frias do inverno.</description>
            <g:image_link>http://www.example.com/image1.jpg</g:image_link>
            <g:price>25</g:price>
            <g:condition>novo</g:condition>
            <g:id>1a</g:id>
        </item>

    </channel>
</rss>
  • What would be "below that"? If you want to put at the end of the file, wouldn’t they actually be below the rest of the content? That part was very confusing.

  • below the xml closure , with the tags Channel and rss

  • You have to [Edit] the question and put how the XML should look and how it comes from feed google?

  • Right, but the link I put up is the google template.

  • But in this template there are already the tags you want to insert. What is the code you are using to generate this new XML and how it is getting?

  • I’ll use a loop to insert database data.

  • Put it to try to make the problem more understandable. For now it is very confusing.

  • is a simple foreach using file_put_contents, the problem is just to put the closure in the last loop

  • 1

    And why don’t you put it out of the loop after it? It will only run when the loop ends.

  • I hadn’t even thought about it, I’ll test it, thank you

Show 5 more comments

1 answer

1

Whereas you intend to add numerous tags <item> followed by </channel></rss> a possible solution is:

<?php
$xmlstr = '<?xml version="1.0"?><rss >...</description>';
// Coloca o início do xml no arquivo
file_put_contents('feed.xml', $xmlstr);

// itens recuperados do banco de dados
$items = [
            "<item><title>título 1</title><description>descrição 1</description>",
            "<item><title>título 2</title><description>descrição 2</description>",
            "<item><title>título 3</title><description>descrição 3</description>",
];

// Coloca os itens no arquivo
foreach ($items as $item) {
        file_put_contents('feed.xml', $item, FILE_APPEND);
}

// Coloca o fim do xml no arquivo
file_put_contents('feed.xml', '</channel></rss>', FILE_APPEND);

// imprime o conteúdo do arquivo na STDOUT
echo file_get_contents('feed.xml');
  • I did the same, thank you for your reply

Browser other questions tagged

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