After some searches on the web and some tests, i can't find the solution and need your help
From a php script, i create a XML output :
$xml = new SimpleXMLElement($string);
//some code
echo $xml->asXML();
I Would like to create a file contain the XML and save on my server by using PHP.
Thanks for your help
if echo $xml->asXML() displays the xml then I guess you could use file_put_contents or other similar function. ie:
$filepath='/path/to/directory/filename.xml';
file_put_contents( $filepath, $xml->asXML() );
Related
This question already has answers here:
How to return a file in PHP
(4 answers)
Closed 3 years ago.
Hi i got an xml file and i want to display it on a website.
I tried everything what i found and tried to do it my self but im a newbie to any code language.
My code is this right now:
<?php
header('Content-type: text/xml');
$xml = simplexml_load_file(xmlfile.xml) ;
echo $xml ;
?>
And the output what i see when i go to my website is nothing just a warning about: This XML file does not appear to have any style information associated with it. The document tree is shown below.
But i cant see anything. So please can someone help me write a code that outputs the whole xml file including xml declaration , tags and node values?
You do not have to use the simplexml_load_file: there is another function to read a file, this function is file_get_contents($filename).
Here is a simple code to use:
<?php
// Set the encoding to XML
header('Content-type: text/xml');
// Get contents of the file
$xml = file_get_contents("xmlfile.xml") ;
// Print contents
echo $xml;
?>
I hope it helped you! And sorry for the language mistakes ;)
Try this code. It works for me.
<?php
header("Content-type: text/xml");
$yourFile = "xmlfile.xml";
$file = file_get_contents($yourFile);
echo $file;
If you insist on simple xml you can write like this.
$xml = simplexml_load_file("xmlfile.xml");
echo $xml->asXML();
Currently I have a PHP file that reads posted XML and then converts/outputs it to JSON. This file looks like this:
<?php
file_put_contents('myxmlfile.xml', file_get_contents('php://input'));
$xmldoc = new DOMDocument();
$xmldoc->load("myxmlfile.xml");
$xpathvar = new DOMXPath($xmldoc);
// Etc etc, for the purpose of my question seeing the rest isn't necessary
// After finishing the conversion I save the file as a JSON file.
file_put_contents('myjsonfile.json', $JSONContent);
?>
The data I'm receiving comes in XML format. To convert it I'm currently saving it as an XML file, and then immediately after creating a new DOMDocument() and loading it in. My question is, is there any way I can cut out the middle man and just load in the XML directly using file_get_contents()?
Ideally it would be this (didn't work):
$xmldoc->load(file_get_contents("php://input"));
If anyone could help me do this I'd really appreciate it!
Thanks
To load from string, instead of filename, use loadXML method.
$xmldoc->loadXML(file_get_contents("php://input"));
this is my question in XML and new to this. i was struggle with creating a xml file using xml parser in php. my requirement is read array which contains data to write in xml. how to process the array for writing data into xml file. i tried to write a single data in xml but i didn't create a xml file otherwise it just display the data in browser.
[1]: http://www.phpbuilder.com/board/showthread.php?t=10356853 i used the code in the link but i didn't create a xml file . how to create .xml file using xml parser in php.
I post here a sample code I use to generate a sitemap.xml file on a website. You may find it useful.
// Init XMLWriter
$writer = new XMLWriter();
$writer->openURI(APPLICATION_PATH . '/public/sitemap.xml');
// document head
$writer->startDocument('1.0', 'UTF-8');
$writer->setIndent(4);
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
// Write something
// this will write: <url><loc>http://www.mysite.com</loc></url>
$writer->startElement('url');
$writer->writeElement('loc', 'http://www.mysite.com');
$writer->endElement();
// end urlset
$writer->endElement();
// end document
$writer->endDocument();
This will create a sitemap.xml file in the public directory. Make sure PHP has writing rights on the target directory. Usually, on Linux+Apache, giving writing rights to the www-data user on this directory does the trick.
In my site, I have implemented the following functionality: If the user clicks on a button it triggers a PHP function which generates an XML file (that PHP function is called by AJAX). Everything is working well, but here's one thing I want to change: I don't want an .XML file to be created on the server machine; instead, I want the user to be prompted to save the .XML file locally. How do I do that? My PHP script currently looks like this:
$xml = new DOMDocument("1.0", "UTF-8");
$rootElement = $xml->appendChild($xml->createElement("SomeNodeName"));
...
// the following doesn't really work - xml doesn't get formatted :)
$xml->formatOutput = true;
// this creates the actual file and places it on server. that's not what i need
$xml->save("MyXMLfile.xml");
Thanks for all the help.
Every web content has a header, so if you specify the header for an xml file (through the use of the header() function with the appropriate code it'll work.
This would mean doing something like this:
<?php
header('Content-type: text/xml');
// set the filename
header('Content-Disposition: attachment; filename="pwet.xml"');
// echo the content here
echo $xml; // simply like this, maybe?
I'm using PHP to extract data from a MySQL database. I am able to build an XML file using DOM functions. Then using echo $dom->saveXML(); , I am able to return the XML from an AJAX call. Instead of using AJAX to get the XML, how would I save the XML file to a spot on the server? Thanks
Use the DOMDocument::save() method to save the XML document into a file:
$dom->save('document.xml');
Doesn't DOMDocument::save() help you?
Use PHP XML DOM Parser to create and save XML file. The following code and tutorial would be found from here - Create and Save XML File using PHP
$xmlString = 'Insert XML Content';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('fileName.xml');
Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:
$strxml = $dom->saveXML();
$handle = fopen("yourxmlfile.xml", "w");
fwrite($handle, $strxml);
fclose($handle);
And you are done.
Remember that the user running your application server (Apache, probably) will need permissions to write in the directory you are placing the XML file.