Dessert #14 - The new Xml class
Posted on 21/9/06 by Felix Geisendörfer
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:
var $name = "XmlTest";
var $uses = array();
$Feed =& new XML('http://www.thinkingphp.org/feed/');
And then display it in the View:
<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.
To be honest, I prefer this XML parsing class (at the moment). I highly recommend it:
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.
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?
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 (http://trac.akelos.org/) 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.
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?
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.
No, I am using v 188.8.131.5244. Version 1.2 isn't out yet, is it? Anyway, I couldn't find place to get it. Any place to download it?
It's not out, no. But you can get it from the 1.2 SVN branch by using svn export.
Thanks for help. That is my first dive into SVN repository:)
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
OK, my above code example obviously doesn't work! ..you can view XML here: http://dev.mincapartments.com/test.xml
thanks a lot for this hint. I am still using 184.108.40.20644 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, del.icio.us and alexa are just a link away ;-)
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
[__parser] => Resource id #38
Just another WordPress weblog
Fri, 10 Nov 2006 05:53:08 +0000
When I use the view file for xml_test/index.thtml
childNodes->childNodes->childNodes as $Node): ?>
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.
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: https://trac.cakephp.org/changeset/3804 .
Thanks to Marcin Domanski aka kabturek (http://kabturek.info) for pointing it out to me via email.
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)
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.
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 = "http://www.youtube.com/a...";
$Feed =& new XML($file);
foreach ($Feed->children->children->children as $Node)
$thumbnail = $Node->value;
Awesome Mandy! Hope you don't spend all day on youtube.com now ... ; ).
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.
Considering that SimpleXml gives quite a bit more functionality, I wonder how much this class would be used (in PHP 5).