Contact Us

Dessert #14 - The new Xml class

Posted on 21/9/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

This is going to be my first 1.2 dessert, I hope the 1.1 people don't mind it, in case you really want to use it in your current application, you can grab the Xml class itself from the 1.2 branch and put it into the vendors folder.

One of the very cool things that's going to ship with the new release is a new Xml class that can parse any given Xml document for you, which is a really nice thing to have, especially if you manually parsed Xml before like I did.

I was thinking about how to show it's functionality best, and I think the good old RSS example will do the job. So in order to fully appriciate it, take a look at the code of my RssModel needed to parse an Rss feed. After that look at a very quick & dirty example of displaying an Rss feed using the new Xml class of 1.2:

Fetch the feed like this in the Controller:

class XmlTestController extends AppController
    var $name = "XmlTest";
    var $uses = array();

    function index()
        $Feed =& new XML('');
        $this->set('Feed', $Feed);

And then display it in the View:

<?php if ($Node->name=='item'): ?>
    <h2><?php $Title = $Node->getChild('title'); echo $Title->value; ?></h2>
        <?php $Description = $Node->getChild('description'); echo $Description->value; ?>
<?php endif; ?>

That's it, you already parsed an entire Rss feed and displayed all it's item in a simple headline/paragraph chain. Now imagine how easy it'll be to work with web services using the power of this class ; ). I'm talking with nate about this kind of stuff right now, and it looks like CakePHP will get a couple new datasources in 1.2 for this as well, but I'll write more about this once the Api for it get's more stable. However, one thing is for sure: We'll not need to extend the Model class any more to emulate different data sources in 1.2.

--Felix Geisendörfer aka the_undefined


You can skip to the end and add a comment.

Mladen Mihajlovic said on Sep 22, 2006:

Considering that SimpleXml gives quite a bit more functionality, I wonder how much this class would be used (in PHP 5).

Ben  said on Sep 22, 2006:

To be honest, I prefer this XML parsing class (at the moment). I highly recommend it:

Felix Geisendörfer said on Sep 23, 2006:

While both of those approaches look nice and probably would be useful to some degree, neither of them could be used in CakePHP. SimpleXml because it's php5 and CakePHP will still support php4 in version 2.0, and the one you pointed out Ben, because it would violate CakePHP's IP policiy to use foreign vendor stuff.

Afaik, that's the main reason the Xml class for CakePHP was written and why it's going to be used.

Ben  said on Sep 23, 2006:

While I can see the point in the CakePHP IP policy, I also think it is dangerous to have the "not invented here" mentality. Is there a document anywhere outlining why they chose this route?

Felix Geisendörfer said on Sep 23, 2006:

Ben: I'm not a CakePHP spokesmen, so don't take my word for it. I just made this statement based on the fact that there is not a single line of code that wasn't written by the CakePHP team in the distribution, as well as by the fact that this is advertised on the front page. They also only accept code contributions by people who signed a CLA. That's all the insight I got into it.

However, this policy absolutly one of my favourite parts about CakePHP. Because that's why CakePHP isn't bloated piece composed of hundreds of OS projects like the Akelos Framwork ( is, which weights over 5mb by default. So personally I don't think the origin of this policy is that they only want stuff invented by the CakePHP core team, but that they want to have the best quality with the least bloat, and that is what I like.

Romas  said on Oct 11, 2006:

Sadly I didn't manage to get this example running:( A short more detailed explanation would be more than welcome... I get the following error: Warning: require_once(cake\libs\set.php) [function.require-once]: failed to open stream: No such file or directory. Of course I couldn't find a file named set.php... should there be one? where can I get one?

Felix Geisendörfer said on Oct 11, 2006:

Romas: Are you using Cake 1.2? If not you might have to copy more then just the xml.php file to your vendors folder and include it manually.

Romas  said on Oct 12, 2006:

No, I am using v Version 1.2 isn't out yet, is it? Anyway, I couldn't find place to get it. Any place to download it?

Felix Geisendörfer said on Oct 12, 2006:

It's not out, no. But you can get it from the 1.2 SVN branch by using svn export.

Romas  said on Oct 12, 2006:

Thanks for help. That is my first dive into SVN repository:)

Richard  said on Oct 24, 2006:


I upgraded to Cake 1.2 to try this out but cannot get to work. I have no problem using your example but cannot parse XML below ..any ideas?








Internet Rate Single







Internet Rate Double







Internet Rate Suite

Richard  said on Oct 24, 2006:

OK, my above code example obviously doesn't work! can view XML here:


Siegfried Hirsch said on Oct 24, 2006:

Hello Felix,
thanks a lot for this hint. I am still using and I wanted to mention, that I had to download the xml.php AND set.php from svn.

After installing xml.php in the vendors directory and using: vendor('xml'); instead of uses('Xml'); and putting the set.php into the right directory cake/libs everything else worked very well.

Makes a lot of sense, because now technorati, and alexa are just a link away ;-)

Eric  said on Nov 13, 2006:

Hi, I install the set.php and xml.php into my cake/lib, and I use , I can get the following display:

CakePHP Rapid Development

XML Object

[__parser] => Resource id #38

[__file] =>

[__rawData] =>
Just another WordPress weblog

Fri, 10 Nov 2006 05:53:08 +0000


... ...

When I use the view file for xml_test/index.thtml
childNodes[0]->childNodes[0]->childNodes as $Node): ?>

name=='item'): ?>

... ...

I can't get the result, instead of
Warning: Invalid argument supplied for foreach() in

D:\Program Files\xampp\htdocs\cake\app\views\xml_test\index.thtml on line 5

Why it happend ?
I have 1.10 installed.

Felix Geisendörfer said on Nov 17, 2006:

I just found out that the childNotes variable has been renamed to children and that my code above won't work any longer. Check out this changeset to see what it's all about: .

Thanks to Marcin Domanski aka kabturek ( for pointing it out to me via email.

Maxim said on Jan 23, 2007:

Will there be one day any xpath function to this xml lib? :'(

(P.S. : the above link to trac points to the changeset "3804." -> notice the dot. It should be "3804" without the dot because it is considered as a float number or something like that)

Felix Geisendörfer said on Jan 27, 2007:

Maxim: Dunno if there will be xpath support. I'm somebody who does not particularly like XML so I'm not missing it. However, I can see how it would come handy if one has to work with XML a lot ...

PS: I fixed the link above - thx.

Mandy said on Feb 22, 2007:

Using the new xml.php file, I was able to get this to work:

I wanted to contact the youtube dev api to get the thumbnail url and for that I did -

$file = "";

$Feed =& new XML($file);

foreach ($Feed->children[0]->children[0]->children as $Node)


if ($Node->name=='thumbnail_url')


$thumbnail = $Node->value;





Felix Geisendörfer said on Feb 23, 2007:

Awesome Mandy! Hope you don't spend all day on now ... ; ).

snowdog said on Nov 23, 2007:

Another change to the core: 'getChild' was replaced by 'child'...

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.