core error

SimpleXML in PHP

Posted on: 15/08/2011


What?

SimpleXML is a PHP extension (enabled by default after PHP 5.1.2) which provides a simple toolset to convert an XML document into an object so it can be easily used by a PHP application

Why?

Before PHP5 handling XML documents was quite painful.  With SimpleXML you just need to create one SimpleXMLElement and from that moment on you can use the XML document as it was a regular object where each node is an attribute containing other attributes/nodes.

How?

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>Back to the Future</title>
<characters>
<character>
<name>Dr. Brown</name>
<actor>Christopher Lloyd</actor>
</character>
</characters>
<rating type="stars">5</rating>
</movie>
</movies>
XML;</code>

$xml = new SimpleXMLElement($xmlstr);
echo $xml->movie[0]->plot;
?> 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


  • None
  • Justin Cooney: Great comic, this made me laugh!
  • Justin Cooney: Interesting, I didn't know about the Google App Engine, I'm glad I found your article on it!

Categories

%d bloggers like this: