PHP XML IE problem - php

<?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.

Related

PHP won't receieve POST XML data

I'm building a service backend that is being sent a "delivery report" after successfully sending a SMS to a user.
The report itself is XML POSTed to our "endpoint" with the content-type application/xml.
I'm using Postman to make sure that everything is working correctly. I've made a test using regular JSON and can return the data without issues, however, no matter what I try with XML I basically get no indication that anything is being sent to the server.
(Test with JSON)
(Test with XML)
Here's my simple PHP script:
<?php
header('Content-Type: application/xml; charset=utf-8');
print_r(json_decode(file_get_contents("php://input"), true));
print_r($HTTP_RAW_POST_DATA);
?>
I feel like I've tried everything. Been looking at past issues posted to SO and other places, it simply won't work for me. I'm hoping for some answers that at least points me in the right direction here.
Cheers.
Your trying to json_decode XML data. You should use something like SimpleXML.
Instead of...
print_r(json_decode(file_get_contents("php://input"), true));
You should use ...
$xml = new SimpleXMLElement(file_get_contents("php://input"));
echo $xml->asXML();
You should be able to get the information by (for example)...
echo (string)$xml->id;
json_decode can't read XML, seems like you're trying to parse XML with json_decode. if you want to output the received XML, use echo (or if it's for debugging purposes, use var_dump, eg var_dump(file_get_contents("php://input"));), or if you want to parse the XML, use DOMDocument.

Magento Controller displaying big XML file causes odd rendering in Browser

I am working on a relatively extensive but not huge XML file that is being delivered from a custom Module in Magento using a controller. Everything has been going well and I have been able to get it to work and add nodes with no issues. The browser (Chrome in this case) has been rendering the XML document fine and stylizing it as expected. In order to display the XML headers properly via Magento I am using the following code:
/* Set display to render output as an XML document */
$this->loadLayout(false);
$this->getResponse()->setHeader('Content-Type','text/xml');
echo "<Magento >";
.
.
.
echo "</Magento>\n";
$this->renderLayout();
When the output reaches 140 nodes, it stops rendering as formatted XML and just shows the data as if it is being rendered as HTML and doesn't know the node definition. Example:
If I comment a node out so there are 139 it will render properly. If I save the full 140+ node XML page as a file then drop that file into Chrome, it renders properly. For Example:
If I run the 140+ node XML file through a validator it comes through as valid XML code. I also get the same result of a broken rendering display if I use Firefox instead to view the page.
The question. Is there a limit as to how big an XML file can be to have it be delivered via a Magento Controller? If so, can I reset this limit to more than 139 nodes so this renders properly? Conversely, should I not worry about if the browser is rendering the page properly because it is going to be used by a different program and not a browser?
Check your header with curl. My guess it it's not being set.
curl -I http://example.com
Try setting your header directly with
header('Content-Type: text/xml');
Setting a header on the response object will only impact content that's delivered via the response object. Since you're echoing code directly, the response object never gets the chance to send its headers.
Both Chrome and Firefox have a set of heuristics that attempt to guess how a document should be rendered. Even with a text/html header a short XML document may be detected as XML, triggering XML rendering. Once it reachers a certain length, the browsers guess that's it's an HTML document, and the HTML rendering engine takes over.
Ensuring your header is set correctly should solve the problem.
Usually this problem happens when you break a tag (not close it) or insert characters not supported. Unknown size limitations of XML, perhaps your browser validation is crashing.

Php adding xml for no reason

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.

Get all content with file_get_contents()

I'm trying to retrieve an webpage that has XML data using file_get_contents().
$get_url_report = 'https://...'; // GET URL
$str = file_get_contents($get_url_report);
The problem is that file_get_contents gets only the secure content of the page and returns only some strings without the XML. In Windows IE, if I type in $get_url_report, it would warn it if I want to display everything. If I click yes, then it shows me the XML, which is what I want to store in $str. Any ideas on how to retrieve the XML data into a string from the webpage $get_url_report?
You should already be getting the pure XML if the URL is correct. If you're having trouble, perhaps the URL is expecting you to be logged in or something similar. Use a var_dump($str) and then view source on that page to see what you get back.
Either way, there is no magic way to get any linked content from the XML. All you would get is the XML itself and would need further PHP code to process and get any links/images/data from it.
Verify if openssl is enable on your php, a good exemple of how to do it:
How to get file_get_contents() to work with HTTPS?

Mediawiki + AJAX + IE = broken

I have created an extension for mediawiki that works in all major browsers other than IE (any version it appears). The extension relies on mediawiki's ajax wrapper to send an xmlhttprequest with parameters that essentially build a database query to a php script. This script will run a query based on the parameters and then create an XML object (using php's simplexml class) which then returns the XML to javascript for display in the browser (just a table, mostly).
Now with all that information, IE seems to be working up until the point at which it tries to parse the returned XML. I have set the mime type to application/xml and I have tried loading it with various different techniques found via google (none worked).
It is trivial to load the XML for parsing when using non-IE browsers:
function callbackHCL(response){
if (response.readyState == 4) {
var xmlObj = response.responseXML;
if (response.status == '200'){
if (xmlObj !== undefined){
//etc...
Now I can start using dom functions to get at data.
My Question: Does anybody have any suggestions on how to parse xml in IE based on my current scenario?
If you would like to email me at tccroninv#gmail.com, I can provide longer code snippets, they are longer and I don't believe they would help the situation. If you would like me to post more code, just ask as well.
Thanks in advance,
Tim
I think this might be what you want: http://dean.edwards.name/weblog/2006/04/easy-xml/. Basically, IE doesn't return an XML document like the other guys. Need to do a little fancy footwork to make it work correctly. I'm sure there's a library out there that wraps this all up so you don't have to worry about it if you don't want to.

Categories