I'm trying to load a DOM using simplexml_load_string
in PHP, but program always terminates right where the call is (nothing after the call gets executed). I've loaded the xml from a file into a string using.
$source = file_get_contents("/path/to/file.xml");
Now, calling echo $source;
returns the full output of the xml file in my browser. This is the source I get in browser:
<xml>
<title>Title</title>
<date>4 April 2015</date>
<content>
//...HTML content...//
</content>
</xml>
However, when the program calls
$xml = simplexml_load_string($source);
the program terminates and anything after that statement doesn't run. I've been through the docs and searched for hours now and can't seem to find anything like this. Before trying this method to construct the DOM I tried using simplexml_load_file
with identical results.
Has anyone seen something like this?
Your XML string is not valid XML. You cannot have an element name that is called "xml" or an element that name starts with "xml". Try renaming your root element i.e. use something like <root>
instead of <xml>
Just in case this helps, I just tried to reproduce your issue, but it worked fine for me.
XML:
<?xml version='1.0'?>
<xml>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</xml>
PHP:
<?php
$source = file_get_contents("./file2.xml");
echo $source;
echo "after source
";
$xml = simplexml_load_string($source);
print_r($xml);
echo "after xml";
?>
Output:
Joe Jane I know that's the answer -- but what's the question? after source SimpleXMLElement Object ( [title] => Forty What? [from] => Joe [to] => Jane [body] => I know that's the answer -- but what's the question? ) after xml