[SOLVED] How can I make a simplexml_load_file readable?

Issue

Hello everyone I’m using simplexml_load_file to load a url so I can get a catalog from a another company. How can I make it so its readable like this:

Catalog website
enter image description here

and not like this

Catalog in php

enter image description here

The catalog that’s on the website is the url that I use in my code, so e.g. https://USERNAME:[email protected]/generator/catalog/xml – I can’t share the company’s real address.

this is how I get the catalog from the company

$url = "https://USERNAME:[email protected]/generator/catalog/xml";
   $xml = simplexml_load_file($url);
   foreach ($xml->PRODUCT as $product) {
   print_r($product);
} 

Solution

What you’re looking for is:


$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->saveXML();

and then for viewing:


echo '<pre>'. print_r($product, true). '</pre>';

You should also be careful with simplexml_load_file(), since it can be affected by https://bugs.php.net/bug.php?id=62577. Instead you can use simplexml_load_string(file_get_contents($filepath));

Answered By – VR T

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.