How to geneate media:content xml using php - php

i facing few issue's i don't know how to resolve it
i want to write following rss feed using php and mysql:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<item>
<title>Here is the title</title>
<link>http://example.com</link>
<media:content medium="image" url="http://example.com/image.jpg"></media:content>
<media:content medium="video" url="http://example.com/video.mp4"></media:content>
</item>
</channel>
</rss>
here is my code in php:
$db = new Db();
$result = $db->select("select * from column");
$xml = new SimpleXMLElement('<channel/>');
for ($i = 0; $i < count($result); $i++) {
$title = $result[$i]title
$link = $result[$i]['link'];
$image = $result[$i]['image'];
$video = $result[$i]['video'];
$item = $xml->addChild('item');
$item->title = $title
$item->link = $link
$item->image = ?? ;
$item->video = ?? ;
}
Header('Content-type: text/xml');
print($xml->asXML());
just don't know how to set image and video because there tags are different like media:content and url of both image and video tags. please let me know
thanks

You can not do that. Your XML is not valid. Prefixed tags must have a declared NameSpace URI, in your case 'http://search.yahoo.com/mrss/':
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
(...)
</rss>
You can use ->addChild to create namespaced elements:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel/>
</rss>');
(...)
$item = $xml->channel->addChild('item');
$item->title = $title;
$item->link = $link;
/* Add <media:image>: */
$node = $item->addChild( 'image', Null, 'http://search.yahoo.com/mrss/' );
$node->addAttribute( 'url', $image );
/* Add <media:video>: */
$node = $item->addChild( 'video', Null, 'http://search.yahoo.com/mrss/' );
$node->addAttribute( 'url', $video );
Result:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<title>Your Title</title>
<link>http://www.somelink.it/</link>
<media:image url="http://www.image.com/"/>
<media:video url="http://www.video.com/"/>
</item>
</channel>
</rss>

Related

How to parse xml file using php script [duplicate]

This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 5 years ago.
This is may xml file
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>werwer</title>
<link>werwerwe</link>
<item>
<g:id>704667</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
<item>
<g:id>4456456</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
</channel></rss>
how to parse that xml file, I have script but it doesnt work
if (file_exists('products.xml')) {
$xml = simplexml_load_file('products.xml');
print_r($xml);
} else {
exit('Failed to open products.xml.');
}
any idea how to get information between g:id?
You need to get an apply the namespace to the children. http://php.net/manual/en/simplexmlelement.getnamespaces.php
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>werwer</title>
<link>werwerwe</link>
<item>
<g:id>704667</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
<item>
<g:id>4456456</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
$ns = $xml->getNamespaces(true);
foreach ($xml->channel->item as $item) {
echo $item->children($ns['g'])->id.PHP_EOL;
}
/*
704667
4456456
*/
https://3v4l.org/t2D84

Delete Selected Items From Google Merchant XML

i want to remove g:price=0 OR out of stock OR no image ITEMS from my Google Merchant xml feed by PHP.
i'm trying for hours and hours; but could not find a solution yet..
example: (if i have xml like this; the new xml must list only the second item)
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title><![CDATA[example title]]></title>
<link><![CDATA[http://www.example.com]]></link>
<description><![CDATA[example description]]></description>
<item>
<g:additional_image_link><![CDATA[]]></g:additional_image_link>
<g:image><![CDATA[]]></g:image>
<g:availability><![CDATA[out of stock]]></g:availability>
<g:price>0.00 TRY</g:price>
</item>
<item>
<g:image><![CDATA[http://www.example.com/image.jpg]]></g:image>
<g:availability><![CDATA[in stock]]></g:availability>
<g:price>100.00 TRY</g:price>
</item>
</channel>
</rss>
Could someone help me? Expected output is this:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title><![CDATA[example title]]></title>
<link><![CDATA[http://www.example.com]]></link>
<description><![CDATA[example description]]></description>
<item>
<g:image><![CDATA[http://www.example.com/image.jpg]]></g:image>
<g:availability><![CDATA[in stock]]></g:availability>
<g:price>100.00 TRY</g:price>
</item>
</channel>
</rss>
Here we are using DOMDocument for extracting nodes and removing un-required nodes.
Try this code snippet here
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
<title><![CDATA[example title]]></title>
<link><![CDATA[http://www.example.com]]></link>
<description><![CDATA[example description]]></description>
<item>
<g:additional_image_link><![CDATA[]]></g:additional_image_link>
<g:image><![CDATA[]]></g:image>
<g:availability><![CDATA[out of stock]]></g:availability>
<g:price>0.00 TRY</g:price>
</item>
<item>
<g:image><![CDATA[http://www.example.com/image.jpg]]></g:image>
<g:availability><![CDATA[in stock]]></g:availability>
<g:price>100.00 TRY</g:price>
</item>
</channel>
</rss>
XML;
$array = array("g:image", "g:price", "g:availability");
$domObject = new DOMDocument();
$domObject->loadXML($string);
$results = $domObject->getElementsByTagName("item");
$nodesToRemove = array();
foreach ($results as $node)
{
foreach ($node->childNodes as $innerNode)
{
if ($innerNode instanceof DOMElement && in_array($innerNode->tagName, $array))
{
if ($innerNode->tagName == "g:image" && empty($innerNode->textContent))
{
$nodesToRemove[] = $innerNode->parentNode;
break;
} elseif ($innerNode->tagName == "g:price" && preg_match("/\b0+(\.[0]+)\b/", $innerNode->textContent))
{
$nodesToRemove[] = $innerNode->parentNode;
break;
} elseif ($innerNode->tagName == "g:availability" && $innerNode->textContent == "out of stock")
{
$nodesToRemove[] = $innerNode->parentNode;
break;
}
}
}
}
foreach ($nodesToRemove as $node)
{
$node->parentNode->removeChild($node);
}
echo $domObject->saveXML();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title><![CDATA[example title]]></title>
<link><![CDATA[http://www.example.com]]></link>
<description><![CDATA[example description]]></description>
<item>
<g:image><![CDATA[http://www.example.com/image.jpg]]></g:image>
<g:availability><![CDATA[in stock]]></g:availability>
<g:price>100.00 TRY</g:price>
</item>
</channel>
</rss>

how to import specific tags from RSS XML into MySQL database

What I'm trying to do is to import a RSS XML feed into a MySQL database using PHPs simpleXML function. But the only thing the PHP script does is writing 0's to my database. What am I doin' wrong?
Sample rss.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/divers/rss/xslt.php" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title></title>
<link></link>
<description></description>
<lastBuildDate></lastBuildDate>
<ttl></ttl>
<item>
<title></title>
<description></description>
<link></link>
<guid></guid>
<pubDate></pubDate>
</item>
</channel>
</rss>
PHP
<?php
include ('config.php');
$xml_file = "http://xml.xml/rss.xml";
$xml_load = simplexml_load_string(file_get_contents($xml_file));
$link = (string)$xml_load->link;
$guid = (string)$xml_load->guid;
$sql = "INSERT INTO table (link,guid) VALUES('$link','$guid');";
mysql_query($sql) or die("Error ".mysql_error());
?>
This is what I was doing:
$sxml = simplexml_load_file($feedURL); // loads in simple xml format
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
$summary = stripslashes($entry->summary);
}
As you can see I use stripsplashes().
I guess it would look like:
foreach($xml->item as $item) {
$link = stripslashes($item->link);
$guid = stripslashes($item->guid);
}

XML reforming with DOM

I am trying to reformat XML adding intermediate level node.
Here is what I have as input:
<channel>
<item>
<title>Advanced PHP Book</title>
</item>
<item>
<title>MySQL primer</title>
</item>
<item>
<title>C++ for beginners</title>
</item>
</channel>
I need it to be like that at the end (page node added between channel and item):
<channel>
<page>
<item>
<title>Advanced PHP Book</title>
</item>
<item>
<title>MySQL primer</title>
</item>
<item>
<title>C++ for beginners</title>
</item>
</page>
</channel>
Here is my testing code:
$sxe = simplexml_load_string($string);
$dom_sxe = dom_import_simplexml($sxe);
$dom = new DOMDocument('1.0');
$channel = $dom->appendChild($dom->createElement('channel'));
$page = $channel->appendChild($dom->createElement('page'));
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $page->appendChild($dom_sxe);
$dom->formatOutput = true;
echo $dom->saveXML();
The problem I have is that channel element is doubled.
Please help.
I don't think this should be too hard: I think you're overcomplicating it by using the simplexml stuff.
$dom = new DOMDocument;
$dom->loadXML($string);
// create the <page> element
$page = $dom->createElement('page');
while ($dom->firstChild->firstChild) {
// move the items in <channel> to the <page> element
$page->appendChild($dom->firstChild->firstChild);
}
// insert the <page> element into <channel>
$dom->firstChild->appendChild($page);
$dom->saveXML();
$xml = '<channel> <item> <title>Advanced PHP Book</title> </item> <item> <title>MySQL primer</title> </item> <item> <title>C++ for beginners</title> </item> </channel>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$page = $dom->createElement('page');
$items = $dom->getElementsByTagName('item');
while ($items->length) {
$page->appendChild($items->item(0));
}
$dom->getElementsByTagName('channel')->item(0)->appendChild($page);
echo $dom->saveXML();
Output
<?xml version="1.0"?>
<channel> <page><item> <title>Advanced PHP Book</title> </item><item> <title>MySQL primer</title> </item><item> <title>C++ for beginners</title> </item></page></channel>
See it.

Generate XML with namespace URI in PHP

Ho to make this "simple" xml with php using DOM? Full code will be welcomed.
<rss version="2.0"
xmlns:wp="http://url.com"
xmlns:dc="http://url2.com"
>
<channel>
<items>
<sometags></sometags>
<wp:status></wp:status>
</items>
</channel>
</rss>
i'm so lost. Code will help me more than any explanation.
Here is the code:
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$rss = $dom->createElement('rss');
$dom->appendChild($rss);
$version = $dom->createAttribute('version');
$rss->appendChild($version);
$value = $dom->createTextNode('2.0');
$version->appendChild($value);
$xmlns_wp = $dom->createAttribute('xmlns:wp');
$rss->appendChild($xmlns_wp);
$value = $dom->createTextNode('http://url.com');
$xmlns_wp->appendChild($value);
$xmlns_dc = $dom->createAttribute('xmlns:dc');
$rss->appendChild($xmlns_dc);
$value = $dom->createTextNode('http://url2.com');
$xmlns_dc->appendChild($value);
$channel = $dom->createElement('channel');
$rss->appendChild($channel);
$items = $dom->createElement('items');
$channel->appendChild($items);
$sometags = $dom->createElement('sometags', '');
$items->appendChild($sometags);
$wp_status = $dom->createElement('wp:status', '');
$items->appendChild($wp_status);
echo $dom->saveXML();
?>
It outputs:
<?xml version="1.0" encoding="utf-8"?>
<rss
version="2.0"
xmlns:wp="http://url.com"
xmlns:dc="http://url2.com"
>
<channel>
<items>
<sometags></sometags>
<wp:status></wp:status>
</items>
</channel>
</rss>
Fore more help: http://us2.php.net/manual/en/book.dom.php
If you want "simple" then use SimpleXML, not DOM. Note that SimpleXML won't indent the XML.
$rss = simplexml_load_string('<rss version="2.0" xmlns:wp="http://url.com" xmlns:dc="http://url2.com" />');
$channel = $rss->addChild('channel');
$items = $channel->addChild('items');
$sometags = $items->addChild('sometags');
$status = $items->addChild('status', null, 'http://url.com');
echo $rss->asXML();

Categories