I’m having a problem loading my xml file using simplexml_load_file(), would really appreciate some help. The code worked a couple of times and after that all I got was unable to open file even though the file exists Thanks.
<?php
echo 'starting read<br>';
////////////////////////////////////
//$categories=$_POST["categories"];
//$project=$_POST["project"];
//$camera=$_POST["camera"];
//$description=$_POST["description"];
//$num=$_POST["val"];
///////////////////////////////////
$categories='a';
$project='a';
$camera='a';
$description='a';
$num=2;
//////////////////////////////////
$data=$categories.'<br />'.$project.'<br />'.$camera.'<br />'.$description;
echo $data;
$file = "main.xml";
//checking if file exists
if(!file_exists($file))
{echo'<br> file does not exist<br>';
}
else
echo '<br> file exists <br>';
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// access XML data
$val=$xml->settings->imagesPage->imagesGallery->pageTitle[$num];
echo $val;
$xml->settings->imagesPage->imagesGallery->pageTitle[$num]=$data;
file_put_contents($file, $xml->asXML());
echo '<br>'. $desc.'\n';
?>
this is the xml file structure
<?xml version="1.0" encoding="iso-8859-1"?>
<content>
<settings>
<imagespage>
<imagesGallery>
<pageTitle><![CDATA[<br>SED UT PERSPI<br>CANON 124<br>LOREM IPSUM<br>DOLOR SIT AMET]]></pageTitle>
<pageTitle><![CDATA[<br>SED UT PERSPI<br>CANON 124<br>LOREM IPSUM<br>DOLOR SIT AMET]]></pageTitle>
<pageTitle><![CDATA[<br>SED UT PERSPI<br>CANON 124<br>LOREM IPSUM<br>DOLOR SIT AMET]]></pageTitle>
<pageTitle><![CDATA[<br>SED UT PERSPI<br>CANON 124<br>LOREM IPSUM<br>DOLOR SIT AMET]]></pageTitle>
<pageTitle><![CDATA[<br>SED UT PERSPI<br>CANON 124<br>LOREM IPSUM<br>DOLOR SIT AMET]]></pageTitle>
</imagesGallery>
</imagespage>
</settings>
</content>
I have also made sure that allow_url_fopen is set to ON
@tlenss thank you i corrected
$descit was supposed to be$data, the other error you found was a typo i made on the webpageMy problem was that my xml file loaded a correctly the first few times but then threw errors saying it was unable to load On closer inspection of
var_dump($xml)i found the problem was that simpleXML cannot handle cdata properlyso i loaded my file like this
loading like this
$xml = simplexml_load_file($file)is fine if your document doesnot have a cdata section