I'm trying to simply save an XML file from the web locally (this part works fine) and then delete a node of the XML and resave it. However, the local xml file ends up blank when I do the following:
$xml = file_get_contents($xmlurl);
file_put_contents('187file.xml', $xml);
$rep187 = simplexml_load_file('187file.xml');
unset($rep187->ComparableSalesReport->ComparableSales->ComparableSale);
file_put_contents('187file.xml', $rep187);
file_put_contents does not accept an object as the second argument (it only accepts a string, an array or a stream resource).
You can pass a string instead by using SimpleXMLElement::asXML on your $rep187 document, like so:
$xml = file_get_contents($xmlurl);
file_put_contents('187file.xml', $xml);
$rep187 = simplexml_load_file('187file.xml');
unset($rep187->ComparableSalesReport->ComparableSales->ComparableSale);
file_put_contents('187file.xml', $rep187->asXML());
Related
so I am trying to edit an xml file using php's simplexml extension but I am getting some problems and its
when I tried
$settings = simplexml_load_file("settings.xml");
....
if(isset($aInformation['cName']))
{
$settings->general->communityname = $aInformation['cName'];
$settings->asXML();
}
but I failed with the saving step...
$settings = simplexml_load_file("settings.xml");
$xmlconfigs = new SimpleXMLElement($settings);
....
if(isset($aInformation['cName']))
{
$settings->general->communityname = $aInformation['cName'];
$xmlconfigs->asXML();
}
but I failed too with the error
String couldn't be parsed to XML...
and I had tried searching on those posts before but they are the same as my failed example codes something edit XML with simpleXML and PHP SimpleXML error update xml file
Second one is not possible as SimpleXMLElement can only take a well-formed XML string or the path or URL to an XML document. But you are passing an object of class SimpleXMLElement returned by simplexml_load_file. That is the reason it was throwing error String couldn't be parsed to XML...
In first one the asXML() method accepts an optional filename as parameter that will save the current structure as XML to a file.
If the filename isn't specified, this function returns a string on
success and FALSE on error. If the parameter is specified, it
returns TRUE if the file was written successfully and FALSE
otherwise.
So once you have updated your XML with the hints, just save it back to file.
$settings = simplexml_load_file("settings.xml");
....
if(isset($aInformation['cName']))
{
$settings->general->communityname = $aInformation['cName'];
// Saving the whole modified XML to a new filename
$settings->asXml('updated_settings.xml');
// Save only the modified node
$settings->general->communityname->asXml('settings.xml');
}
I have my DOMDocument saved as $xml.
I have the node loaded, I also have a custom field i've made which is called 'field_xml_file'.
But I can't quite seem to get how to save the file and insert it into the node.
I currently have this:
$fileName = 'file.xml';
$file = file_save_data($xml, 'public://', $fileName);
$newRevision->field_xml_file[LANGUAGE_NONE][] = (array)$file;
node_save($node);
Any help?
$fileName is not supposed to be third parameter. Change that line to:
$file = file_save_data($xml, 'public://'.$fileName);
second parameter is full file name, using drupal stream wrappers.
also, if this is literally code you are using, you need to change $newRevision to $node in order to save changes
This question already has answers here:
PHP HttpRequest
(3 answers)
Closed 9 years ago.
I have this in file called abc.php, and this will return a valid xml document, instead of showing -string- labels at the end and beggining
header('Content-type: application/xml');
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
echo $xmlstr;
I want to use the valid xml data of abc.php, extract certain data, store it in my db, and check the output of the other server periodically, I've tried this:
ob_start();
include 'abc.php';
$result = ob_get_clean()
as well as this:
$xml = file_get_contents("abc.php");
$xmlstr = simplexml_load_string($xml);
without success, any advice?
Make sure you output the MIME Type as well, or else the server will feed text/html to it and it will be all wrong. Put this function
header("Content-type: application/xml");
in abc.php so the client will recognize it as XML.
file_get_contents("abc.php") will return you the contents of the file "abc.php"; it will not execute that PHP code. The include with output buffering trick ought to do roughly the right thing, but I'm not sure why you'd ever want to do it that way, so it's not worth working out why that's failing.
If you can access the code in abc.php, then simply make it into a PHP function, which returns the processed XML:
function get_the_actual_xml()
{
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); // External web service
$xml_obj = simplexml_load_string($xml); // Load into SimpleXML object
return (string)$xml_obj; // Convert contents back to a string
}
If for some reason your two PHP files need to be on different servers, you will need to reference the URL to abc.php, not just where it is on disk. That way, the PHP code will be executed, and what you'll get back is the result of that echo statement. If your server has the allow_url_fopen setting enabled, this is as simple as $remotely_processed_content = file_get_contents('http://sanjosecostarica.org/test/abc.php')
at the end I couln't get the results of "abc.php" but only the content, I try a different approach successfully:
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
$xmlok = <<<XML
$xmlstr
XML;
$xml = simplexml_load_string($xmlok);
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 a simple code written (based on some tutorials found around the internet) to parse and display an XML file. However, I only know how to reference an XML file stored on my server and I would like to be able to use an XML file that is being returned to me from a POST.
Right now my code looks like this:
if( ! $xml = simplexml_load_file('test.xml') )
{
echo 'unable to load XML file';
}
else
{
foreach( $xml as $event)
{
echo 'Title: ';
echo "$event->title<br />";
echo 'Description: '.$event->info.'<br />';
echo '<br />';
}
}
Is there some way I can replace the simpleXML_load_file function with one that will allow me to point to the POST URL that returns the XML file?
Use simplexml_load_string instead of loadfile:
simplexml_load_string($_POST['a']);
If you get the url to the file in the POST you can propably use the simplexml_load_file function with the url, but if that doesn't work you can use the file_get_contents in combination with the simplexml_load_string:
//say $_POST['a'] == 'http://example.com/test.xml';
simplexml_load_file($_POST['a']); // <-- propably works
simplexml_load_string(file_get_contents($_POST['a'])); //<-- defenitly works (propaly what happens internally)
also getting contents of external files could be prohibited by running PHP in safe mode.
If you are receiving a file that's been uploaded by the user, you can find it (the file) looking at the content of the $_FILES superglobal variable -- and you can read more about files uploads here (for instance, don't forget to call move_uploaded_file if you don't want the file to be deleted at the end of the request).
Then, you can work with this file the same way you already do with not-uploaded files.
If you are receiving an XML string, you can use simplexml_load_string on it.
And if you are only receiving the URL to a remote XML content, you have to :
download the file to your server
and, then, parse its content.
This can be done using simplexml_load_file, passing the URL as a parameter, if your server is properly configured (i.e. if allow_url_fopen is enabled).
Else, the download will have to be done using curl -- see curl_exec for a very basic example, and curl_setopt for the options you can use (you'll especially want to use CURLOPT_RETURNTRANSFER, to get the XML data as a string you can pass to simplexml_load_string).
From http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=php4:
If you do not want to save the
uploaded file directly but to process
it, the PHP functions
file_get_contents() and fread() can
help you. The file_get_contents()
function returns a string that
contains all data of the uploaded
file:
if (is_uploaded_file($_FILES['myFile']['tmp_name']))
$fileData = file_get_contents($_FILES['myFile']['tmp_name']);
That will give you a handle on the raw text within that file. From there you will need to parse through the XML. Hope that helps!
Check out simplexml_load_string. You can then use cURL to do the post and fetch the result. An example:
<?php
$xml = simplexml_load_string($string_fetched_with_curl);
?>