Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created December 20, 2012 12:14
Show Gist options
  • Save martinbean/4344973 to your computer and use it in GitHub Desktop.
Save martinbean/4344973 to your computer and use it in GitHub Desktop.
Building a Google RSS feed with DOMDocument.
<?php
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$rss = $xml->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:g', 'http://base.google.com/ns/1.0');
$xml->appendChild($rss);
$channel = $xml->createElement('channel');
$channel->appendChild($xml->createElement('title', '[Removed]'));
$channel->appendChild($xml->createElement('description', '[Removed]'));
$channel->appendChild($xml->createElement('link', '[Removed]'));
$rss->appendChild($channel);
foreach ($products as $product)) {
$item = $xml->createElement('item');
$item->appendChild($xml->createElement('title', $product['title']));
$item->appendChild($xml->createElement('description', $product['title']));
$item->appendChild($xml->createElement('link', $product['url']));
$item->appendChild($xml->createElement('g:id', $product['product_id']));
$item->appendChild($xml->createElement('g:price', $product['price_latest']));
$item->appendChild($xml->createElement('g:brand', $product['range']));
$item->appendChild($xml->createElement('g:condition', 'new'));
$item->appendChild($xml->createElement('g:image_link', $product['image']));
$channel->appendChild($item);
unset($item);
}
$xml->save('products.xml');
@EdEichman
Copy link

You need to escape '>', '<', and '&' characters in the strings passed to createElement with HTML escape sequences.
$text = str_replace('&','&', $text);
$text = str_replace(array('>', '<'), array('>','<'), $text);
Otherwise, Martin, you code was a great help, thanks!

@harkalygergo
Copy link

Thanks Martin, it was really useful, but I found an extra closing bracket at line 17 (foreach).

Plus I created an external function for items, maybe it would be useful for others too:

function getXmlItem($xml, $channel, $g_id, $g_title, $g_description, $g_link, $g_image_link, $g_additional_image_link, $g_availability, $g_price, $g_unit_pricing_measure, $g_unit_pricing_base_measure, $g_identifier_exists, $g_brand): void
    {
        $item = $xml->createElement('item');

        $item->appendChild($xml->createElement('g:id', $g_id));
        $title = $xml->createElement('g:title');
        $title->appendChild($xml->createCDATASection($g_title));
        $item->appendChild($title);

        $item->appendChild($xml->createElement('g:description', $g_description));

        $link = $xml->createElement('g:link');
        $link->appendChild($xml->createCDATASection($g_link));
        $item->appendChild($link);

        $item->appendChild($xml->createElement('g:image_link', $g_image_link));
        $item->appendChild($xml->createElement('g:additional_image_link', $g_additional_image_link));
        $item->appendChild($xml->createElement('g:availability', $g_availability));
        $item->appendChild($xml->createElement('g:price', $g_price));
        $item->appendChild($xml->createElement('g:product_type', $g_price));
        $item->appendChild($xml->createElement('g:unit_pricing_measure', $g_unit_pricing_measure));
        $item->appendChild($xml->createElement('g:unit_pricing_base_measure', $g_unit_pricing_base_measure));
        $item->appendChild($xml->createElement('g:identifier_exists', $g_identifier_exists));
        $item->appendChild($xml->createElement('g:brand', $g_brand));

        $channel->appendChild($item);
        unset($item);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment