Tag Archives: XML

PHP: Does SimpleXMLElement have toString() method? No, better use asXML()!

SimpleXML is a PHP extension that “provides a very simple and easily usable toolset to convert XML to an object” [1]. Thus you can pass a link to an XML file and SimpleXML will return an object.

$xml = simplexml_load_file('path_to_the_file');

Sometimes you’d need to dump or save the entire XML as a string, but there’s no toString method! As you can see $xml is an instance of the SimpleXMLElement class.

var_dump($xml); // object(SimpleXMLElement)...

Actually if you take a closer look:

$xml->toString();

will return “Call to an undefined method toString()”, which is frustrating because the developers community is used to use toString() when converting an object into a string.

XML
PHP's SimpleXML doesn't have toString() method. asXML() is used instead!

The solution

In fact there’s a method doing exactly what’s needed. This is SimpleXMLElement::asXML

As described in the manual page: “SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element” [2].

Besides that it does exactly what’s needed it sounds irrelevant, because you’ve an XML object and the name “asXML” doesn’t describe correctly what’s expected.

$xml->asXML() // ?!
  1. SimpleXMLElement::asXML
  2. SimpleXML Introduction

Media RSS and ZF – Part 2

Here’s the promised chunk of code making the most simple bridge between Zend Framework and Media RSS.

Step 1

Just add a simple action in a controller:

class IndexController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    $this->getResponse()->setHeader('Content-Type', 'application/xml');
	    $this->view->somevar = 'some value';
 
	    echo $this->view->render('index/index.xml');
 
	    $this->_helper->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	}
}

Step 2

After you’ve setup the xml file as a view script – you can simply access it as a normal Zend Framework view:

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:av="http://www.searchvideo.com/schemas/av/1.0">
  <channel>
   <title><?php echo $this->somevar ?></title>
   ...
   other media rss elements
   ...
</channel>
</rss>

Zend Framework and Media RSS

The Problem

Since native RSS format is not designed to deliver media content, there is a need for newer format supporting video and audio. There is such format – Media RSS introduced by Yahoo! in 2004. Actually now RSS supports some king of pseudo audio and video with the enclosure tag.

Inserting Media In RSS with Media RSS

Although there is a standardized format, there is not a Zend Framework native module. There is however a proposal, but that’s far not enough.

Possible Solutions

For me there are two possible solutions. The first is writing Zend_Feed_Mrss and extend the Zend_Feed_Rss functionality, but that appears to be a non-trivial task. The second solution is to write custom view returning XML, which I’m choosing for now. I hope I can paste some code soon!

Cross-browser rounded corners! Works on IE but, but not on Opera!

What’s this that works on IE and any other browser, except on Opera?!

Rounded Corners

That was a strange answer. Who’s making rounded corners with CSS?! on IE and more important how? There is a way to make it, but not entirely with CSS and HTML. The solution is with VML a subset of XML to deal with vectors, and CSS of course.

The original solution I found on snook.ca.

It is really working fine as described and shown in the example, but there are some issues as well. There is no way to setup background image on the container, and the width and height are behaving strange.

However this gives you an opportunity to make cross browser rounded corners with no scripting that slows down the page and except Opera it’s quite working!