Page 1 of 1

PHP echoing xml bck to ajax

Posted: Sat Oct 24, 2009 12:31 pm
by jeffmatthews
:banghead:

This is killing me.

It worked for so long, and now, it doesn't. This is an xml file. I found file_get_contents to be more reliable, so I went that route. It worked for days, up until last night. I have no clue what I changed, and what it used to be before I broke it. Whatever it was, it had to be minor.


<?php

header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';

$cfile = $_GET["q"];
$cfile=$cfile.'.xml';

$xml = file_get_contents($cfile); //simplexml_load_file

echo $xml;
return;

Re: PHP echoing xml bck to ajax

Posted: Sat Oct 24, 2009 12:50 pm
by jeffmatthews
I found the issue, but still need some help dealing with it.

When, I open my xml file in Excel and edit it, and then save it back, Excel puts encoding tags in it as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

The <state xmlns.... is supposed to just be a plain-old <state> tag. There should not be any encoding in there at all.

If I delete this junk, it works.

My question now is, how do I prevent Excel from loading my file like that? Or, alternatively, how can I get PHP to deal with the crap that Excel loads?

FYI, the code is now corrected back as follows, but it does not like the excel junk in my file:


<?php
$cfile = $_GET["q"];
$cfile=$cfile.'.xml';

header('Content-Type: text/xml');

$xml = file_get_contents($cfile); //simplexml_load_file($cfile);

echo $xml;
return;

Re: PHP echoing xml bck to ajax

Posted: Sat Oct 24, 2009 6:20 pm
by requinix
Look into SimpleXMLElement::getDocNamespaces. Can't hurt to add a bit of robustness to your script.

(SimpleXML assumes that the URI of the default namespace is "". Excel specified otherwise - nothing wrong, actually, just uncommon.)