Saturday, January 7, 2012

SimpleXML, Birds Eye View

XML is always been a pain to me. The way I learn the best is by looking at examples. Below is a script from a project that I was recently working on using php SimpleXML object. It shows an organize format of the XML file to serve as a road map


<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie page="0" numpages="38">
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;



//Output
$xml = new SimpleXMLElement($xmlstr);

echo '<pre>';
print_r($xml->movie);
echo '</pre>';
echo 'Example:' . $xml->movie[0]->title;

?>


Script above will output


SimpleXMLElement Object
(
[@attributes] => Array
(
[page] => 0
[numpages] => 38
)

[title] => PHP: Behind the Parser
[characters] => SimpleXMLElement Object
(
[character] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Ms. Coder
[actor] => Onlivia Actora
)

[1] => SimpleXMLElement Object
(
[name] => Mr. Coder
[actor] => El ActÓr
)

)

)

[plot] =>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.

[great-lines] => SimpleXMLElement Object
(
[line] => PHP solves all my web problems
)

[rating] => Array
(
[0] => 7
[1] => 5
)

)
Example:PHP: Behind the Parser

No comments:

Post a Comment