Write data to xml using XML Parser in PHP - php

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.

Related

How to Download XML File from a web service with HTML like code

I am new to PHP. I am downloading an XML file from a web service using PHP. I can download the file using this code:
$sourcefile = "http...com?querystring=string";
$destinationfile = 'data\description.xml';
$xml = file_get_contents($sourcefile);
file_put_contents($destinationfile, $xml);
But when I open the XML file, it has < where < should be and > where > should be.
I added this line of code to decode it before saving it to file, which fixes the above problem:
$xml = html_entity_decode($xml);
This doesn't seem to me to be the right way to go about it. Also, I am getting a weird piece of text showing up in the XML file, which prevents me from parsing the XML file:

I tried using str_replace($xml) right before decoding it (and tried it after decoding it), but that wouldn't get rid of it.
What is the correct way to download an XML file using GET from a web service in PHP and will it get rid of that weird string ()?

DOMDocument - Directly load XML file from php://input

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

How does xml editing works in PHP?

If I open a XML file in PHP and edit part of it, does PHP load whole XML content then edit part of it then save whole content back to the file?
It depends on which XML library you are using, but for most of them: yes, they'll load the entire XML into memory (unless you're using XMLReader or XML Parser), and for writing there is XMLWriter which can output XML in portions which you can manually write to a file, the rest will create the string as a whole and can possibly save that to the file directly, or give you the whole string which you can save to a file yourself.
As for saving to file: there is no way PHP can 'edit' a 'portion' of a file. PHP can append to a file, or rewrite the whole file. There is no mechanism I'm aware of that just edits a portion.

Create and Save XML file to Server Using PHP

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.

how to generate a xml file which is validating to a .xsd file in php?

How to generate a xml file which is validating to a .xsd file in php? More explanation: I have some source xml files which has to be convert into other xml file with validating to a xsd file.content will be same as source xml in new xml file but new xml should validate with a new xsd file.
pictorial:
Source XML ->***validate(.xsd)file(xsd file contains 4000lines of xml tag)*
**-> generate New xml file(New xml content will come from source xml file)...
If you know any online tool also tell me otherwise if it is possible in php also please explain me..
Please can you help anyone???????
Thanks in Advance..

Categories