Can't write with php simpleXML to web page

This code shows nothing in my website:

note.xml
This code shows nothing in my website:

note.xml

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>

[off-topic]
When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.
[/off-topic]

Have you tried checking what’s in $xml after you have loaded it from the file, using var_dump() or similar? If I try a version of your code using simplexml_load_string it works.

I think, correct is $xml->note->to and so on…

I wondered about that, but I didn’t need to.

<?php
$a = '<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Dont forget me this weekend!</body>
</note>';

$xml = simplexml_load_string($a);

echo $xml->to;

?>

produces “Tove”. If I echo $xml->note->to I get nothing.

I removed the ' from “Don’t” because I couldn’t remember how to escape it.

Could be. Long time not work with SimpleXML and lazy to check it up.

Entity. I mean &apos; or &#39;

Actually… Exactly that gonna be a OP problem. He should check it up.

Why don’t we start with the basics before speculating too much. Let’s see if the function is telling us what is wrong. (simplexml_load_file will throw E_WARNING for each XML error found.)

Put this at the top of your page, and reload it:

<?php
ini_set('display_errors', "1");  
error_reporting(E_ALL);  
?>

I just checked. the code works at http://chesstao.com/test-1.php but it doesn’t work in localhost.

Is there a setting in php.ini I can use to allow it to work?

Fatal error: Uncaught Error: Call to undefined function simplexml_load_file() in /mnt/base/www/chesstao/test-1.php:10 Stack trace: #0 {main} thrown in /mnt/base/www/chesstao/test-1.php on line 10

LIne 10:
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");

This only fails in localhost, it works perfectly at http://chesstao.com/test-1.php

How can I fix this? I changed the permissions to 755 at web site of test-1.php and note.xml as well as the localhost versions of those files. But it still fails at localhost.



https://www.php.net/manual/en/simplexml.requirements.php

You need libxml extension and actually PHP >=5.

I am using Linux MInt 20 with php 7.4

I searched sudo gedit /etc/php/7.4/apache2/php.ini but there was no libxml string.

https://www.php.net/manual/en/libxml.installation.php
The libxml extension is enabled by default, although it may be disabled with –disable-libxml .

What can you suggest?

If your script tells you: Call to undefined function, that means either extension failed or you use namespace and should to call \simplexml_load_file() (with backslash).

What shows your phpinfo()? There should be information about libxml.


libxml
libXML support	active
libXML Compiled Version	2.9.10
libXML Loaded Version	20910
libXML streams	enabled

simplexml is not the same as libxml. SimpleXML Requires libxml, but is not included as part of it.

How did you install PHP?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.