How does xml editing works in PHP? - 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.

Related

PHP: save a file (generated by aspx) to server

I have an url provided by a wholesaler. Url generates xml file which I need to save on my server.
I use PHP - file_get_contents and file_put_contents to do that:
$savepath = "path_to_my_server_folder";
$xmlurl = "http://usistema.eurodigital.lt/newxml/xmlfile.aspx?xml=labas&code=052048048048051057049050048049052";
file_put_contents($savepath.'eurodigital.xml', file_get_contents($xmlurl));
File is generated on my server, but its content is empty. I have no problems with other xml files if I provide direct xml url, but in this situation file is generated by aspx file dynamically. Xml url is actual url I use. When I open xmlurl in browser, xml file gets saved to device.
Can you help me with moving xml to my server? What function should I use? Is it even possible to do that using PHP 5? "allow_url_fopen" is ON.
Thank you in advance!

Change encoding of an xml file in php

I have opened a server which is waiting for queries.
When i send a query, the server will answer it and safe the results in an xml file.
Problem is, that I can't use this xml properly because in the first line of the xml is now written:
xml version="1.0 encoding="utf-8"
if I change this in the editor into:
xml version="1.0 encoding="iso-8859-1
than it works fine.
But instead of using editor and my hands, i want php to do it?
Thx
Just to close the question:
I used str_replace(str,'utf-8','iso-8859-1') in the xml file, which helped me to open the xml file in the right way.
Thx to you

PHP Parse a PHP file echoing XML data

I have a php file which generates and echo's XML data.
So basically it shows a XML but it's a PHP file.
I need to read and parse this data.
I've seen this: simplexml_load_file('some.xml'); but in this case I cannot do this as I've got the xml as a php file.
How can I do this?
Just the same way, the file extension doesn't matters (.xml or .php) what it really matters is the actual contents of the file, so if your file have a .php extension but its contents are valid xml then you should have no problem:
simplexml_load_file('somepage.php'); //this is fine
Set mime type to xml header('Content-Type:text/xml');

file_get_contents not extracting full file contents

I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.

PHP Print Included XML File

I'm trying to echo out an XML File via a PHP File. Like so:
Url visits viewxml.php?id=1
Php open a specific XML file, such as "xmlfile_".$_GET['id'].".xml" and prints it as if they are visiting the xml file itself.
The reason I'm using PHP is to do Session checks to make sure they are allowed to view the xml file.
This is how I am doing this:
header("Content-type: text/xml; charset=utf-8");
// send the saved XML file.
include "xmlfile_".$id.".xml";
Doing this gives me the following GC XML error:
And there is nothing below it.
I have tried different approaches, such as printing it or turning it into a string but then I have problems reading the XML on certain software. I am hoping one of you can help me.
Thanks!
include() will attempt to evaluate the file; if there's something in there that looks like PHP then it will corrupt your results.
Try using readfile instead; this will only output the file contents.

Categories