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
Related
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');
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.
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.
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) );