I am a beginner in PHP and I know nothing about XML manipulation. I am working on a Google CSE annotation XML shown below:
<?xml version="1.0" encoding="UTF-8" ?>
- <Annotations>
- <Annotation about="http://sanspace.in/">
<Label name="_cse_byxamvbyjpc" />
</Annotation>
- <Annotation about="http://blog.sanspace.in/">
<Label name="_cse_byxamvbyjpc" />
</Annotation>
- <Annotation about="http://google.com/">
<Label name="_cse_exclude_byxamvbyjpc" />
</Annotation>
</Annotations>
I want achieve this from the above shown file:
<?xml version="1.0" encoding="UTF-8" ?>
- <Annotations>
- <Annotation about="http://sanspace.in/">
<Label name="testString1" />
</Annotation>
- <Annotation about="http://blog.sanspace.in/">
<Label name="testString2" />
</Annotation>
- <Annotation about="http://google.com/">
<Label name="testString2" />
</Annotation>
</Annotations>
So far, I have tried:
<?php
if (file_exists('test.xml'))
{
$xml = simplexml_load_file('test.xml');
}
else
{
exit('Error.');
}
foreach($xml->Annotation as $annotation)
{
if ($annotation["about"]=="http://sanspace.in/")
{ $annotation->Label["name"]="testString1"; }
else
{ $annotation->Label["name"]="testString2"; } }
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
$dom->save("test.xml");
?>
This code performs the task but it doesn't save it into the file.
My question is, what's wrong with the $dom->save("test.xml");
statement?
How do I save the XML file on the server?