0

I am learning SimpleXML in PHP. Then I am doing simple test with SimpleXMLElement(...), I dont get anything back. Let me explain. Here is XML file:

<?xml version="1.0" encoding="UTF-8"?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <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>

And here is my php file:

<?php
$xml = simplexml_load_file('example.xml');
echo $xml->getName() . "<br>"; // prints "movies"

$movies = new SimpleXMLElement($xml);
echo $movies->getName() . "...<br>"; // doesnt print anything, not event dots

echo $movies->movie[0]->plot; // even this does not print anything
?>

Only output is:

movies

Please read the comments in php file. I am trying to print xml elements in exact same way after loading file and after doing new simpleXML object. Some how it prints only first echo command results. I searched many examples and could not make it work. Where is the mistake? It is big puzzle for me, but maybe a tiny one for you.

Randel
  • 421
  • 6
  • 11
  • Your script crashes but you do not notice this. Enable error reporting for your development, also log errors to file: http://stackoverflow.com/q/845021/367456 - that will help you a lot in getting forward as it won't happen unnoticed again. – hakre Jun 22 '14 at 00:05

4 Answers4

2

simplexml_load_file already returns your SimpleXMLElement object. Try this:

<?php
$xml = simplexml_load_file('example.xml');

echo $xml->getName() . "<br>";
echo $xml->movie[0]->plot . "<br>\n";
?>
Scott Joudry
  • 883
  • 10
  • 25
  • I php manual, there are only examples with SimpleXMLElement, but they dont load .xml file. Most examples what I looked from other places also included this SimpleXMLElement. At the beginning it is all confusing, because then you learn language, you use example to test yourself and add more figures to make it more intresting. – Randel Jun 20 '14 at 16:42
1

change this line:

$movies = new SimpleXMLElement($xml);

to this:

$movies = new SimpleXMLElement($xml->asXML());
Vaidas
  • 141
  • 1
  • 7
  • While this works, it doesn't actually make any sense; it turns the object into a string of XML, and then back into the same object again. You could in fact nest it as many times as you like: `$movies = new SimpleXMLElement( (new SimpleXMLElement($xml->asXML())->asXML() );`, or more simply `simplexml_load_string( simplexml_load_string($xml->asXML())->asXML() )`. – IMSoP Jun 22 '14 at 22:06
1

What you are trying to do doesn't make much sense, because you are trying to load the same XML twice:

// this loads the XML from a file, giving you a SimpleXMLElement object:
$xml = simplexml_load_file('example.xml');
// this line would do what? load the XML from the XML?
$movies = new SimpleXMLElement($xml);

There are two functions for loading XML in the SimpleXML extension, both return SimpleXMLElement objects:

  • simplexml_load_file - takes a filename, and loads the XML in that file; with the right PHP settings, you can also give it a URL, and it will load the XML straight from there
  • simplexml_load_string - takes a string of XML that you've already got from somewhere else, and loads that

The third way of getting a SimpleXMLElement is calling the class's constructor (i.e. writing new SimpleXMLElement). This can actually act like either of the above: by default, it expects a string of XML (like simplexml_load_string), but you can also set the 3rd parameter to true to say that it's a path or URL (like simplexml_load_file).

The result of all three of these methods is exactly the same, they're just different ways of getting there depending on what you currently have (and, to some extent, how you want your code to look).

As a side-note, there are two more functions which do take an object of XML you've already parsed: simplexml_import_dom and dom_import_simplexml. These are actually pretty cool, because the DOM is a standard, comprehensive, but rather fiddly and verbose way of acting on XML, whereas SimpleXML is, well, simple - and using these functions you can actually use both with very little penalty, because they just change the wrapper of the object without having to re-parse the underlying XML.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
0

try this

<?php
$movies = simplexml_load_file('sample.xml');

foreach($movies as $key=>$val)
{
        echo $val->title.'<br>';
        echo $val->plot.'<br>';
        echo $val->rating[0];
        echo $val->rating[1];
}

?>
Vijayaragavendran
  • 726
  • 1
  • 10
  • 21