<?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.
Related
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"));
I want to be able to create a XML File, add nodes and such to it, then output it to the screen without saving.
$bookxml = new DOMDocument('1.0', 'utf-8');
is how i have the XML file created, however i just can't anyway to display the XML file on the screen without saving it.
However i am having a problem with outputting even in save, this is the line i have
echo $bookxml->save("testing.xml");
All this does is return the file size of the newly created XML, and not the contents.
Any help would be awesome, i'm completely stumped on this.
What you're looking for is saveXML, additionally you can use htmlspecialchars to encode the xml so you can see it in your browser display.
echo htmlspecialchars($bookxml->saveXML());
You want the saveXML method, not the save method:
http://us3.php.net/manual/en/domdocument.savexml.php
My fellow friend is building site in flash and he uses XML files to access data in Flash.
I want to build editable CMS so that client can edit stuff.
Now I don't' have any experience with XML.
I know PHP, HTML and Mysql very well.
So how can I change those already build XML files using Mysql and PHP?
Maybe going through
http://library.creativecow.net/articles/brimelow_lee/php_mysql/video-tutorial.php
will clear things for you.
Though, use it only to understand the concepts of XML and how it relates to mysql, php and swf. For real work look at libraries that deal with XML such as serializer mentioned in AvatarKava's answer.
Output the XML using PHP in exactly the same way the example XML file does and then put this at the top of your code:
header('Content-type: text/xml');
To create the XML file from the database just ouput the data the way you normally would adding XML tags in the right place. Eg:
<news>
<?
while($item = mysql_fetch_array($data)){
?>
<item>
<url><?=$item['url']; ?></url>
<title><?=$item['title']; ?></title>
</item>
}
?>
</news>
If you need more assistance, provide the XML file that was given to you with the flash file as a reference.
You probably should look at the PEAR XML Serializer Package. It makes it easy to convert a multi-dimensional array into XML.
Here's a decent tutorial: http://articles.sitepoint.com/article/xml-php-pear-xml_serializer
may be i am going to ask some stupid question but i don't have any idea about php
that's why i want to know it i never worked in php and now i have to do it so please provide me some useful tips,
i have XML file that is coming from a different URL and i want to save it on the server then i have to read it and extract it to a page in proper format and some modification in data.
You can use DOM
$dom = new DOMDocument();
$dom->load('http://www.example.com');
This would load the XML from the remote URL. You can then process it as needed. See my previous answers on various topics using DOM. To save the file to your server after your processed it, you use
$dom->save('filename.xml');
Loading the file with $dom->load() will only work if you have allow_url_fopen enabled in your php.ini. If not, you have to use cURL to download the remote file first.
Maybe this should be helpfull to you: http://www.php.net/manual/en/function.simplexml-load-file.php
If you're have dificulte to get the XML file from the remote host you can use combine with above simplexml-load-string
$path_to_xml = 'http://some.com/file.xml';
$xml = simplexml_load_string( file_get_content($path_to_xml) );
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.