I tried many ways to save xml file on my filesystem, but it doesn't work. I don't know what can I do...
I only want to read an xml file, then modify the value of a node, and then save this file... But nothing happened. In PHP I don't know what the problem. In Java okay... but I need to do in PHP.
XML file:
<?xml version="1.0"?>
<node>
<pass>test</pass>
</node>
public static function saveToXML()
{
$xml = simplexml_load_file(dirname(__FILE__).'/../../../../sms_data.xml');
$xml->pass = "000";
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
}
The $xml is contains the modified values. It is good, but the save function doesn't wanna work!
I tried this too: echo $dom->save('text.xml'); But nothing. It doesn't create the text.xml file... I don't know... I have been searching for the solutions on google for one day. But I don't know what the hell is going to always wrong. I didnt get nothing. The server log is empty about it...
I am so sorry to ask this. But I don't understand this code why doesn't work... why doesn't save the modification on filesystem level!
Check if the directory exists and is writable.
Related
I gotta be doing something stupid here, cause this code ain't workin
$semXML = new DOMDocument();
$semXML->load('<rss></rss>');
$midXML = $semXML->saveXML();
$uploads = wp_upload_dir();
file_put_contents($uploads['path'].$title.'.xml', $midXML);
It generates an XML file, but the only thing inside it is:
<?xml version="1.0"?>
any help is appreciated, been at this project for two days straight now o_O
Try
$semXML->loadXML('<rss></rss>');
to load XML from a string. Just running ->load(...) expects a file as parameter.
I am using SimpleXML to write to my XML file on my Apache Server. Here is my PHP code:
<?php
$xmlFile = 'http://localhost/database.xml';
//$xml = new SimpleXMLElement($xmlFile, NULL, TRUE);
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents($xmlFile, $xml->asXML());
?>
My XML file code:
<Usernames>
<User>TestUser1</User>
</Usernames>
The problem I am having is that SimpleXML WILL NOT write to my XML file. I have tried many different methods ($xml->asXML($xmlFile), DOMDocument ... ->save) and none of them are working. I changed the permissions on my file and STILL I cannot write to it:
I have spent hours today trying to get this to work with no success. If anyone has any type of solution it would be great to hear.
When you write the contents to the file, you should pass a system filepath as the first variable, your $xmlFile variable is a URL. Change this to the local file name and it should save.
Based on your comments, the following should work
<?php
$xmlFile = 'http://localhost/database.xml';
$xml = simplexml_load_file($xmlFile);
$xml->addChild("User", "TestUser2");
file_put_contents('/Applications/MAMP/htdocs/DataBase/database.xml', $xml->asXML());
But, I would double check the $xmlFile URL - from what you have said, your local URL could be http://localhost/DataBase/database.xml - you should check that you can open your XML file in Safari using the $xmlFile URL.
I have an XML doc that I need to load with PHP. I am currently using the simplexml_load_file() function, however the xml file is malformed, and consequently I am getting a parse error.
The XML file looks something like this:
...
</result>something1>
</else>
</else>
</resu
...
As you can see, this XML is whack and this function is throwing an error trying to parse it. Also I don't need this data that is corrupted. I would just like to read in the stuff that I can and throw everything else away.
As Jonah Bron suggested, try DOMDocument::loadHTML():
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
libxml_use_internal_errors(true);
$dom->loadHTML($xml);
#Juliusz
You don't actually need to set the strictErrorChecking for this I don't think. I tried the following and it seems to work fine. To ignore the errors you need to set the libxml_use_internal_errors(true). Essentially you want to use DOMDocument instead of simplexml. I tried the following and worked without any problems:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<cmd>login</cmd>
<login>Richard</login>
</else>
</else>
</document>
XML;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($string);
print $dom->saveHTML();
?>
Thusjanthan Kubendranathan
try to tidy it up, it worked well for me.
http://hu2.php.net/manual/en/intro.tidy.php
I have created a XML file using PHP's simple XML, saved the file. When opening the file in php using fopen and printing the contents. my XML looks like this: (see below)
<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>
I want the xml file looking all indented and on new lines for each element. Does anybody know how to do this?
Thanks
You can do this using the formatOutput property of DOMDocument.
Save your XML like this instead, presuming your XML is in a variable called $yourXML, and you want to save it to a file at $xmlFilePath:
$dom = new DOMDocument();
$dom->loadXML($yourXML);
$dom->formatOutput = true;
$formattedXML = $dom->saveXML();
$fp = fopen($xmlFilePath,'w+');
fwrite($fp, $formattedXML);
fclose($fp);
Code adapted from here.
This is called "pretty printing" and SimpleXML does not do that. If you search on Stack Overflow and elsewhere on the web you'll find custom solutions that do that.
Pretty printing is good for visulation but I don't recommend saving documents in that format.
If you're still looking for a pretty-printer, you can try SimpleDOM's asPrettyXML()
include 'SimpleDOM.php';
$home = simpledom_load_string('<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>');
echo $home->asPrettyXML();
echo "\n"; for new line in xml
ob_start(); echo '
' . "\n";?>
I'm using PHP5 to create XML files. I have code like this:
$doc = new DOMDocument();
...
$xml_content = $doc->saveXML();
The problem is that created XML code starts with a root node like this one:
<?xml version="1.0"?>
But I want it to be like this:
<?xml version="1.0" standalone="yes" ?>
I guess I need to call some function on $doc, but I can't figure out which one?
You want to set
$doc->xmlStandalone = true;
It's not a function of the class, it's a property so it's a little harder to find in the docs. You can read about it here.