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();
Related
I tried to remove the attribute "xmlns:xhtml" from the tag "xhtml:link" with the following code:
Source Code:
$doc = new DOMDocument('1.0', 'utf-8');
$url = 'android-app://com.domain.name';
$element = $doc->createElementNS($url,'xhtml:link');
$attribute = $doc->childNodes->item(0);
//echo '<br>tag: '.$doc->getElementsByTagName("xhtml:link")[0];
$element->setAttribute('href', $url);
$element->setAttribute('rel', 'alternate');
//echo '<pre>';print_r($element);echo '</pre>';
$element->hasAttributeNS($url, 'xhtml');
$element->removeAttributeNS($url, 'xhtml');
$doc->appendChild($element);
echo $doc->saveXML();
OutPut:
<?xml version="1.0" encoding="utf-8"?>
<default:link href="android-app://com.domain.name" rel="alternate"/>
But, I am expecting the output looks like:
<?xml version="1.0" encoding="utf-8"?>
<xhtml:link href="android-app://com.domain.name" rel="alternate"/>
Please help me what I have to do? Here I struck to replace the tag...
Thanks!
Try createElement function instead of createElementNS:
$doc = new DOMDocument('1.0', 'utf-8');
$url = 'android-app://com.domain.name';
$element = $doc->createElement('xhtml:link');
$attribute = $doc->childNodes->item(0);
$element->setAttribute('href', $url);
$element->setAttribute('rel', 'alternate');
$doc->appendChild($element);
echo $doc->saveXML();
OUTPUT:
<?xml version="1.0" encoding="utf-8"?>
<xhtml:link href="android-app://com.domain.name" rel="alternate"/>
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>
I am trying to create an xml dynamically using domDocument.
What I would like to obtain is following:
<?xml version="1.0"?>
<books>
<book>
<content>
<name><![CDATA[dddd]]></name>
</content>
</book>
</books>
Unfortunatelly the script below does not show the output as excepted.
$xml = new DOMDocument('1.0');
$xml->formatOutput = true;
$books = $xml->createElement('books');
$xml->appendChild($books);
$book = $xml->createElement('book');
$books->appendChild($book);
$inside = $xml->createElement('content');
$book->appendChild($inside);
$xml->appendChild($inside)->appendChild($xml->createElement('name'))->appendChild($xml->createCDataSection('dddd'));
echo '<xmp>'.$xml->saveXML().'</xmp>';
This is the output
<?xml version="1.0"?>
<books>
<book>
<content>
<name><![CDATA[dddd]]></name>
<lastname><![CDATA[dddd]]></lastname>
....
</content>
I do not know how to use createCDataSection().
Like this?
$xml = new DOMDocument('1.0');
$xml->formatOutput = true;
$books = $xml->createElement('books');
$xml->appendChild($books);
$book = $xml->createElement('book');
$books->appendChild($book);
$content = $xml->createElement('content');
$book->appendChild($content);
$name = $xml->createElement('name');
$content->appendChild($name);
$name->appendChild($xml->createCDataSection('dddd'));
echo '<xmp>'.$xml->saveXML().'</xmp>';
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);
}
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.