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.
Related
I am beginner in PHP, XSLT. Found solution for transform XML using XSLT:
$xml = Array2XML::createXML('Document', $result);
$xsl = new DOMDocument;
$xsl->load('Teema.xsl');
$processor = new XSLTProcessor();
$processor->importStyleSheet($xsl);
$results=$processor->transformToXML($xml);
$results=$processor->transformToUri($xml,"NewTeema.xml" );
But, what to do if I have 2 or more XMLs?
This $xml is not file, and I dont want to save each xml, like file on server (because it is was converted response json). Any ideas?
In the XSLT you can load additional document using the document() function.
Another possibility is to register a PHP function that loads the file and returns the value or DOM node.
Good solution, but I decided to merge 2 json, like this:
json_encode(array_merge(json_decode($result,
true),json_decode($resultProducts, true)));
And the use = Array2XML::createXML('Document', $result);
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() );
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"));
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
print $xmlDoc->saveXML();
?>
In the above code sample I don't understand the last line. why we use saveXML(). Is there anyway to print the xml file without using saveXML(). Please anyone can answer my questions.
If you are loading the xml from a file and you are not modifying it in any way, there's no need to call saveXML(). saveXML is mostly used when you create an xml document from scratch.
If you only want to check the contents of the file, you could use file_get_contents, instead of using DomDocument. DomDocument is more useful when you want to make changes or parse.
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.