I am using WAMPServer 2.1 on my local machine. I am trying to create a simple PHP script to output an XML file. I cannot get it to work without generating an error on IE and Chrome. On Firefox 4, it works.
Here is the PHP:
<?php
main();
function main()
{
header("Content-type: application/xml");
OutputXML('xml/login_user_good.xml');
exit(0);
}
function OutputXML($filename)
{
echo file_get_contents($filename);
}
?>
Here is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<login_user>
<result_code value="1"/>
<error value="Invalid username or password"/>
</login_user>
When I tried to access my PHP script using IE 8, I get this error:
The XML page cannot be displayed Cannot view XML input using style sheet.
Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
Invalid at the top level of the document. Error processing resource
'http://localhost/kiamobile/login_user.php'. Line 1, P...
<?xml version="1.0" encoding="utf-8" ?>
^
On Google Chrome 10, I get this error:
This page contains the following errors:
error on line 1 at column 6: XML declaration allowed only at the start of the document
On Firefox 4, I get a properly formatted XML output.
On any of the browsers, when I View Source, I can see the correct XML.
I have tried with and without the explicit header. If I remove the call to the PHP header() function, I get these results:
IE 8: Same error as before
Firefox 4: Blank screen
Chrome 10: Blank screen
Again, if I View Source in any of the browsers, I can see the XML. I have tried using text/html instead of application/xml, with no effect.
I have tried generating the XML directly in the PHP code, like this:
<?php
main();
function main()
{
header("Content-type: application/xml");
OutputXML();
exit(0);
}
function OutputXML()
{
echo <<<END
<?xml version="1.0" encoding="utf-8" ?>
<login_user>
<result_code value="1"/>
<error value="Invalid username or password"/>
</login_user>
END;
}
?>
When I do this, I get these results:
IE 8: Same error
Firefox 4: I see the formatted XML
Chrome 10: Blank screen
What am I doing wrong?
Your script may contain unwanted and invisible chars (like BOM) before PHP start tag
Browsers are not made for displaying just any arbitrary XML. They like HTML (which can be XHTML). It is unclear why you want browsers to display those. If it is intended for an and user then you should format those in HTML, if it is for a API (service) then check it with curl/wget/etc and I'm sure the XMl will look the same every time.
Related
This question already has answers here:
Display XML content in HTML page
(6 answers)
Closed 8 years ago.
I have a similar question to this:
https://superuser.com/questions/140242/why-my-browsers-display-xml-files-as-blank-pages?rq=1
I don't see my xml in my browser, but just a blank page. I only checked Chrome and firefox, but it's happening in both.
The difference is that I'm getting data from MySQL and echoing it. So the file extension is .php and not .xml
$row = mysqli_fetch_assoc($resource);
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<video>\n";
echo "<path>" . $row['path'] . "</path>\n";
echo "</video>\n";
I see a blank page when I go to the url. But when I "View Source", I see the xml tree.
<video>
<path>URLtoVideo.mp4</path>
</video>
My main goal is to grab the xml data and request it in Flash using URLRequest(). But I get this error
TypeError: Error #1088: The markup in the document following the root element must be well-formed.
I think this may be the problem to the error I get in Flash. I only have one root node, and I don't think I have any problems with the child nodes. So my only assumption is that the error occurs because of the blank page.
Try adding an XML header before your output. Your browser is trying to read it like HTML otherwise
header('Content-type: text/xml');
The XML is not displayed because the browser interpret it.
When you write
<table></table>
In your HTML page, you will not see the string
"<table><table\>"
But an empty table.
This is the same things about XML content.
Another Stack overflow post got an answer to this HERE
Here I copy past the answer of mellamokb
Simple solution is to embed inside of a element, which will
preserve both the formatting and the angle brackets. I have also
removed the border with style="border:none;" which makes the textarea
invisible.
Here is a sample: xxxx
You need to make and xml object/element and then echo it, plus dont forget to add header('Content-Type: text/xml'); before echo.
For more refer this link http://php.net/manual/en/simplexml.examples-basic.php
I am using Mink with the Goutte webdriver trying to replace the contents of a form in a website with an XML feed.
I coded the following method:
public function replaceField($field)
{
$baseText = '<?xml version="1.0" encoding="UTF-8"?>
<RiskAssessmentReply xmlns="http://test.com" >
<!-- ExternalId of the Order -->
<OrderId>TO_REPLACE</OrderId>
<RiskInfo>
<Actions>
<SystemAction>SystemAction</SystemAction>
<FinalAction>FinalAction</FinalAction>
</Actions>
<Score SystemScore="0"/>
</RiskInfo>
<!-- One of Accept, Manual_Accept, Reject, Cancel, or Ignore -->
<ResponseCode>Accept</ResponseCode>
<StoreId>TESTSTORE</StoreId>
</RiskAssessmentReply>';
$textWithOrderId = preg_replace('/TO_REPLACE/', $GLOBALS['ORDER_ID'], $baseText);
$this->getSession()->getPage()->fillField($field, $textWithOrderId);
}
Which basically contains the XML feed, then I replace a part of it with an order ID that I have from beforehand and call the function fillField which comes bundled with Mink.
The problem is that it does not just paste the text that I provide, but formats it in a weird manner by setting backslashes before the " symbols, like this:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
Therefore, when I try to submit the XML feed, the website displays the following error:
| Fatal error: Uncaught exception 'ErrorException' with message 'SimpleXMLElement::__construct(): Entity: line 1: parser error : String not started expecting ' or "'
I've tried using the stripslashes method from PHP, but it doesn't work, as if I try an echo after adding it an Order ID it displays the original XML without slashes, so I'm guessing there is a calling to some other function when using fillField that does indeed add the backslashes to my text, but I haven't been able to find the source for it.
Does anyone know where this conversion from " to \" is made in order to avoid it?
Thanks
Make sure the Magic Quotes setting is off in PHP, especially on the server you're submitting to. They are removed as of PHP 5.4.0.
Magic Quotes
Ensure that you are using stripcslashes instead of stripslashes in the page that receives the form.
Basically I am trying to add Google Checkout order processing (level 2 integration) into a CakePHP app using the Google Checkout PHP sample code.
I can successfully create carts and receive notifications sent from Google to my app, however it cannot acknowledge these properly.
The function in google's code to do this echo's this:
<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />
However Google Checkout receives that code like this:
<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />
I can't work out what is causing this, I don't think it has anything to do with Cake and I've disabled PHP short tags so I can use inline XML but this makes no difference.
If I remove the first < from the string, the function echo's the rest of the code correctly, leave it in and it brakes (obviously this also applies for the ? and closing > symbol too, I just cant't have all of them at once!).
I have been able to replicate this myself in attempt to remove any unseen stuff being done in the Google Code using the function below. It does exactly the same thing but is contained within a (Cake) controller and hard codes the variables.
public function blank($tags = null) {
$schema = 'http://checkout.google.com/schema/2';
$serial = '119963104284921-00001-7';
if ($tags != null) {
$acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
} else {
$acknowledgment = '?xml version="1.0" encoding="UTF-8" ?>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
}
$this->set('_ack', $acknowledgment);
}
The IF statement is used to show the difference the < symbol makes.
Calling it with nothing ($tags == null) gives this output:
<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />
Source.
Calling it with any other value ($tags != null) gives this output:
<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />
Source.
My question (finally!) is, why does this happen? and how can I get it output the XML correctly?
Sorry if I've missed something really obvious, but I'd rather have it pointed out here than faff around getting nowhere for another day!
this "problem" has to do with the PHP interpreter.
There are solutions but everything in View not in the Controlled.
//var
$questionmark = "?";
echo "<".$questionmark."xml version=\"1.0\" encoding=\"utf-8\"".$questionmark.">";
// separate
echo '<' . '?xml version="1.0" encoding="utf-8"?' . '>';
You forgot to close the quotes on the xmlns attribute, that's the probable cause. Try this:
$acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.'" '.'serial-number="'.$serial.'" />';
Some XML parsers do not allow specifying the encoding in capital letters (e.g. UTF-8 is incorrect).
Try changing the encoding to "utf-8" to see if that fixes it.
I am trying my first AJAX and having a problem with my xml receiving function.
I alert responseText and I can see the xml returned from my server, but when I try and get responseXML I get null and the error.
Here is the php function that builds my xml
header('Content-type: application/xml');
echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
echo("<results>");
echo("<table><![CDATA[tablereererere]]></table>");
//echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>\n");
//echo("<table>".$_POST['table']."</table>\n");
//echo("<post_id>".$_POST['post_id']."</post_id>\n");
//echo("<user_id>".$_POST['user_id']."</user_id>\n");
//echo("<rating>".$_POST['rating']."</rating>\n");
echo("</results>");
And here is my javascript function which is processing the returned xml
function ajaxReceiver(http_request) {
//this function continues to run until a result is returned and then it creates the new div
if(http_request.readyState == 4) {
response_xml = http_request.responseXML;
response_text = http_request.responseText;
alert(response_text);
alert(response_xml.getElementsByTagName("table")[0].textContent);
//document.getElementById('floatingNotification').innerHTML = response_text;
// alert(http_request.responseXML.getElementsByTagName("table")[0].textContent);
//ratedTable = responseXML.getElementsByTagName("table").value;
//alert(ratedTable);
//message = response.getElementsByTagName('table')[0].textContent;
//alert(message);
//alert(message);
//this response contains the xml document that was returned by the php function.You can get any values out of the xml document and
//use javascript dom to manipulate the contents on the page
}
}
It may be because, even though you're setting the content-type correctly, you need to have an <xml tag in the top of your response. Also, you aren't closing your last tag properly. This should work:
echo("<?xml version='1.0'?>");
echo("<results>");
echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>");
echo("<table>".$_POST['table']."</table>");
echo("<post_id>".$_POST['post_id']."</post_id>");
echo("<user_id>".$_POST['user_id']."</user_id>");
echo("<rating>".$_POST['rating']."</rating>");
echo("<message>$message</message>");
echo("</results>");
For more info on how to define your XML: http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
You are closing the parent node improperly (</results> not <results/>) and you should (after sanitizing it) wrap all the POSTDATA in <![CDATA[...]]> tags to be safe. Make sure it's UTF8 encoded, too (see utf8_encode())
EDIT: and what wajiw said about the <?xml version="1.0" encoding="UTF-8" ?> tag at the beginning.
Edit: Example CDATA Block Usage
<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
<myData><![CDATA[
Now I just throw in my data, for fun and profit!
This way I can use special, reserved characters like <, > and &!
]]></myData>
</myNode>
Edit Yet Again:
Why not give Content-Type: text/xml, NOT application/xml, a go?
The answer is to put the asynchronous property in the function "open()" to false.
Like this:
ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();
I had the same problem and couldn't solve it until I put my results into subnodes, e.g.
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<summary>$summary</summary>\n");
echo("<content>$content</content>\n");
didn't work for me for some weird reason, but this does:
header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<page>\n");
echo(" <summary>$summary</summary>\n");
echo(" <content>$content</content>\n");
echo("</page>\n");
My code retrieving the answer is
function retrieveRequest(title)
{
if (_xmlRequest.readyState == 4 && _xmlRequest.status == 200)
{
var xmlResponse = _xmlRequest.responseXML;
_divSummary.innerHTML = xmlResponse.getElementsByTagName("summary")[0].textContent;
_divContent.innerHTML = xmlResponse.getElementsByTagName("content")[0].textContent;
}
}
I had this error happen to our team once and it took me a long time before we realized that the problem was in our XML data returned from the server. In particular, PHP script that generated return XML string was the culprit.
My solution was to remove any white space from the beginning of the PHP script. I mean any spaces, new lines, and/or tabs need to be removed from the beginning of the script, so that the first thing in the script is the <?php tag itself. It turned out that the <?PHP tag was not the first thing on the first line of my PHP script; somehow I started my code on the second line and the first script line was simply an empty one.
It drove me mad and it took some time for me to figure this out, so I hope someone else can benefit from this solution. It is very simple and easy to try if all else fails.
I have been trying to output XML with PHP but encountered a strange(!) error in Internet Explorer.
The expected xml output is this:(simplified)
<root>
<match_id>12</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>453</fan_id>
</root>
I am producing this output with the following PHP code:
echo "<?xml version='1.0' encoding='utf-8' ?>
<root>
<match_id>"; echo $match->getId(); echo "</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id>".$_SESSION['user_id']."</fan_id>
</root>";
In Firefox, the output is same as expected. However, in IE, the output is this:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<match_id>0</match_id>
<stadium_id>43</stadium_id>
<tribune_id>2</tribune_id>
<fan_id />
</root>
This is a really annoying error. I have set the PHP header for XML output, and changed lots of other things but could not make it work.
The $match->getId() part is just returning an integer but IE always shows this value as 0. If I set <fan_id> and <match_id> manually, IE shows the values correctly.
By the way, I am using this XML output in Flash (AS3) and this also shows the same result with IE.
What am I doing wrong?
This looks like it's due to a session difference - the IE session isn't storing the user id. Similarly, $match->getId() is actually 0 - I imagine you'd get a similar result using Chrome or Safari or a web browser on any other computer.
One other thing: Flash may not be sending the PHP session cookie to the server on the request - which would match the IE behavior / no valid session.
Try:
header( "content-type: application/xml; charset=ISO-8859-15" );
OR
$doc = new DOMDocument;
$root = $doc->createElement('root');
$doc->appendChild($root);
$match_id = $doc->createElement('match_id', $match->getId());
$root->appendChild($match_id);
$stadium_id = $doc->createElement('stadium_id', '43');
$root->appendChild($stadium_id);
$tribune_id = $doc->createElement('tribune_id', '2');
$root->appendChild($tribune_id, '2');
$fan_id = $doc->createElement('fan_id', $_SESSION['user_id']);
$root->appendChild($fan_id);
echo $doc->saveXML();
//$doc->save('file.xml'); // if you want to write to file
What browser is used has no effect on how your PHP executes (because it's executed on server, not in browser). Notice that in the second example your fan_id is also empty, which indicates something's wrong with your session setup. Investigate this.
Having said all that: did you consider using simplexml() to output XML from PHP? It's much more fun to use than echoing tags.