Save XML not working, or I'm just too tired - php

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.

Related

XML saving doesn't work on filesystem

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.

Incorrect elements appended in xml file

I have an xml file running in my server. While everything is working fine in my localserver, but when I upload it on my server, and when many people are using it, I get an error in my xml file.
I am using simplexml in php to read and update data.
For example,
I have the following structure of my xml file
<?xml version="1.0"?>
<db>
<uid></uid>
<score></score>
</db>
While it works perfectly for some time, but after some time, some elements get appended in my xml file. For example,
<db>
<uid></uid>
<score></score>
</db>/score</db>
or
<db>
<uid></uid>
<score><//score>
</db>
or
<db>
<uid></uid>
<core></score>
</db>
and then I get parsing error. Is it because many people are writing to my xml file at the same time? I am using LOCK_EX so that shouldn't be a problem
This is how I am opening it-
$data= new SimpleXMLElement($file, null, true);
And this is how I am closing it-
file_put_contents($file, $data->asXML(), LOCK_EX);
And it works perfectly all right in my localserver. And it works fine for some time when I upload it online but then suddenly weird things happen to my xml file. What's wrong?
Edit:
My update code-
<?php
include('functions.php');
$winid= $_GET['wid'];
$loseid=$_GET['lid'];
$winid=intval($winid);
$loseid=intval($loseid);
$file="data.xml";
$data= new SimpleXMLElement($file, null, true);
$winner=intval($data->score[$winid]);
$loser=intval($data->score[$loseid]);
$exp_winner=expected($loser,$winner);
$new_win=win($winner,$exp_winner);
$exp_loser=expected($winner,$loser);
$new_lose=loss($loser,$exp_loser);
$data->score[$winid]=$new_win;
$data->score[$loseid]==$new_lose;
file_put_contents($file, $data->asXML(), LOCK_EX);
header("Location: index.php");
?>
Try changing all occurrences of $data->score['string'] to $data->score->string. See if it helps!
Example: change $winner=intval($data->score[$winid]); to $winner=intval($data->score->$winid);
Also, you have accidentally wrote two equal signs on this line $data->score[$loseid]==$new_lose;!

PHP: SimpleXML writing issue

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.

Parse a PHP file as an XML file?

I have been working on a project that displays data in an XML file. (It's kind of like an API). I know how to parse XML with PHP, and how to make an XML file in PHP, but they don't work together. :)
Basically, I have two files: parse.php and xml.php.
xml.php grabs info from a MySQL database, and outputs it as XML.
parse.php loads and parses xml.php and outputs it as HTML.
If I run parse.php, it does not load xml.php. However, if I copy the outputted XML (from xml.php) and save it as a xml.xml file (and change the filename in parse.php to 'xml.xml') it works. I'd really appreciate any help.
Content of parse.php:
<?php
$doc = "xml.php";
$doc = #simplexml_load_file($doc) or die("Server Error: Recipe not found!");
$title = $doc->title;
echo $title
?>
Content of xml.php:
<?php
header("Content-type: text/xml");
$dbc = mysql... //gets data from database
echo "<!DOCTYPE..."; //xml stuff here
echo "<title>" . $dataFromMySQL . "</title>";
?>
The database connection works, and the DOCTYPE in the XML is ok, so that's not the problem.
Again, I only get the problem when I generate XML dynamically using PHP. If it's a .XML file, it works fine.
Can anyone help me?
Thanks.
simplexml_load_file will try to actually load the php contents of the xml.php file. It will not run that file first. You need to do some rewriting or use this ugly solution:
ob_start();
include 'xml.php';
$xml = ob_get_clean();
$doc = simplexml_load_string($xml);
//...
NOTE: I like #lonesomeday's proposed solution better, it will just require more rewriting.
#simplexml_load_file($doc);
That is where your problem is. This does not execute xml.php, but attempts to parse that file -- the PHP code that you've written -- as XML. Obviously (since it isn't XML) this won't work.
You have to find a way of getting the output from executing xml.php into parse.php.
The easy way to do this would be to change all your echo calls into $xml .= calls, and simply include xml.php into parse.php.
// xml.php
$xml = '';
$xml .= "<!DOCTYPE..."; //xml stuff here
$output .= "<title>" . $dataFromMySQL . "</title>";
// parse.php
include('xml.php');
simplexml_load_string($xml);
Note that your problem here shows the foolishness of using the error suppression operator #. If you hadn't used it, PHP would have shown you various errors which would have helped you to realise what the problem was.
Addendum: it occurs to me that the best way actually is to forget about the pointless XML step along the way and just convert the database output into HTML.
If you want to do this without rewriting xml.php, you can get PHP to process the file by accessing via url:
$doc = file_get_contents("http://localhost/xml.php");
You're literally loading the local file. Unless you evaluate it, the code doesn't run, so you'll just get the code itself.
You could use CURL to download xml.php over HTTP, or you could make the XML-generation component of the xml.php a callable function which you simply include and execute.
parse.php:
<?php
include('xml.inc');
$doc = #simplexml_load_string(xml_function()) or die("Error");
echo $doc->title;
xml.php:
<?php
include('xml.inc');
header("Content-type: text/xml");
echo xml_function();
xml.inc:
<?php
function xml_function() {
$dbc = mysql... //gets data from database
$xml = "<!DOCTYPE..."; //xml stuff here
$xml .= "<title>" . $dataFromMySQL . "</title>";
return $xml;
}
But... even that seems silly, honestly, when you could have both output methods connect to the same data and skip a generation/parse step. Simply output HTML/XML conditionally.

PHP SimpleXML new line

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";?>

Categories