I am calling a database query and using a foreach loop to display results, however it is causing a W3C error and on inspection I have found that it is adding '<?xml version="1.0" encoding="UTF-8"?>' to my first echo...I have no idea why?? I have moved my php from the very top of the file to the body but still the same problem :(
Can someone help me?
Where are you seeing the <xml ... > header? Note that if you are viewing this in the browser source, the browser will generally automatically add this if the doctype is XHTML.
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'm using this code snippet to geocode an address using Google Geolocation API:
<?php
$address = "Frazione LevĂ - 16030 Sori (GE)";
$url = 'http://maps.google.com/maps/api/geocode/xml?address='.urlencode($address).'&sensor=false';
echo $url."\n";
$xml = file_get_contents($url);
var_dump($xml);
?>
The echo command shows the URL called is:
http://maps.google.com/maps/api/geocode/xml?address=Frazione+Lev%C3%A0+-+16030+Sori+%28GE%29&sensor=false
and the var_dump command shows the result is:
string(107) "<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>ZERO_RESULTS</status>
</GeocodeResponse>
"
So it seems the address cannot be geolocated.
If I call exactly the same URL above ( http://maps.google.com/maps/api/geocode/xml?address=Frazione+Lev%C3%A0+-+16030+Sori+%28GE%29&sensor=false) from my browser, I get a totally different result (the address is correctly geolocated):
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
...
How this is possible and how I can fix it? I need to geolocate addresses from PHP.
Please note I'm using the same code snippet in a loop to geolocate hundreds of addresses every day, and it works fine on most of them; only on some addresses it shows issues like that.
Found the solution here https://stackoverflow.com/a/10302440/2717254 thread found thanks to the StackOverflow related questions suggested in the sidebar :-)
Issue was the Region Biasing. I added ?region=it to the URL and results are now coherent.
Before adding that param, probably Google guessed it me when I loaded the page in the web browser because of headers sent by my browsers (in italian Language on Win7 italian). When I make the request from PHP, less headers are sent and probably Google thinks this is an en-US PC or something similar... :)
Also removing the ZIP code from the address also gives me coherent result across my PHP script and my web browser, but I think the ?region=it is the most clean and logically correct solution.
In my case I solved it with language instead of region: language=it (https://developers.google.com/maps/faq#languagesupport)
I'm trying to load the contents of what should be an rss feed in a .ashx file on a client's site. I'm not sure if this is possible. Here is the code I'm using.
$doc = new DOMDocument();
$doc->load($client_url);
echo htmlentities($doc->saveXML());
All that is being echoed is <?xml version="1.0"?> which I believe is being created from the saveXML() function.
Any help?
Thanks,
Caleb
I've found the problem thanks to hakre and looking at the errors. The problem was that I was using 'https' in $client_url when I should use 'http'.
Thanks for the advice in turning on my error messages. I think this will be very useful in the future as well.
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.
<?xml version="1.0" ?>
<NBR>
<resultGroups>
<result>Hello</result>
</resultGroups>
</NBR>
i have a n xml created in PHP ike this. i am retruning this XML into javascript and is trying to acces the value of node "result" using
alert($(xmlObj).children('result').text());
In firefox its working fine. but in IE it gives out nothing...
how can i solve this???
It could be you are not sending the correct XML Content-Type header. You should send Content-Type: text/xml with XML content. You should also check that you set the correct charset in both the headers and the file. IE should be able to parse a correct XML response just fine if the JS code is correct and your XML response is valid.
My guess would that is has something to do with the text() function. I am a Prototype guy myself but I ran into a similar problem with IE a little while ago. It came from me trying to pull the textContent value from an element in the DOM. I could grab the value in all other browser but IE was giving me the shaft. After running some tests, this is what I came up with:
IE does not support element.textContent. In most browsers, like FireFox, you would be able to pull the textContent value from element.
Example
<p id="my_element">this is my element</p>
alert($('my_element').textContent); // will alert "this is my element"
In IE, you need to use element.innerHTML. This will return the value you want. Right now, I assume that text() is returning the textContent value and that is why you're getting no dice.
Example
<p id="my_element">this is my element</p>
alert($('my_element').innerHTML); // will alert "this is my element"
Hope this helps!
Are you loading this across SSL?
There is a known issue in IE where sometimes it fails to load XML over SSL:
http://support.microsoft.com/default.aspx?scid=kb;en-us;272359
This page contains more info on how to resolve it:
http://www.blog.lessrain.com/flash-nasty-xml-load-bug-in-internet-explorer/
Ensure that your in your headers that you're specifying the Content-Type and charset, and that your charset (if using utf-8) is utf-8 and not utf8. IE doesn't recognise the latter and doesn't bother to tell you.
So, you want your header to specify the following:
Content-Type: application/xml; charset=utf-8
Have you tried parsing the data out before alert'ing it, and possibly using find instead?
var node_text = $(xmlObj).find('result').text();
alert(node_text);
Otherwise i would suggest trying to change the result tag to something different (like theresult) - everyone knows IE likes to do strange things! :)
If <result> is inside <resultGroups>, then try:
alert($(xmlObj).children('resultGroups').children('result').text());
or, you could try:
alert($($($(xmlObj).children('resultGroups')).children('result')).text());
or even:
alert($($(xmlObj).children('result')).text());
Let me know if any of these work.