views:

263

answers:

1

This is a follow up question to this answer:

http://stackoverflow.com/questions/948610/how-do-i-parse-xml-from-php-that-was-sent-to-the-server-as-text-xml

JavaScript/jQuery:

var xmlDoc = jQuery.createXMLDocument( "<items></items>" );
jQuery( "items", xmlDoc ).append( jQuery( "<item>My item!</item>" ) );
jQuery.ajax({
    url: "test.php",
    type: "POST",
    processData: false,
    contentType: "text/xml",
    data: xmlDoc,
    success: function( data ) {
     alert( data );
    }
});

PHP:

$xml_text = file_get_contents("php://input");
$xml = simplexml_load_string($xml_text);
echo $xml->ITEM;

In order for this to work I have to use $xml->ITEM. If I use $xml->item it will not work. I was wondering why when the item node gets appended it is lowercase, but when I retrieve it, it now has to be uppercase? I would like it to be lowercase in my PHP. Thanks.

+4  A: 

Your PHP script receives <items><ITEM>My item!</ITEM></items> as input. Try

<?php
$xml_text = file_get_contents("php://input");
file_put_contents('test.log.txt', $xml_text);
if you think this isn't so.

xmlDoc is an xml document, but the result of jQuery( "<item>My item!</item>" ) isn't. Along the way jQuery uses .nodeName which behaves like .tagName for elements.
https://developer.mozilla.org/en/DOM/element.tagName says:

In XML (and XML-based languages such as XHTML), tagName preserves case. In HTML, tagName returns the element name in the canonical uppercase form. The value of tagName is the same as that of nodeName.

VolkerK
Great Explanation +1
ichiban
So I guess the question still remains, how I can get my PHP to do a case insensitive search on the ITEM node itself? I've been searching for an XPath solution or a SimpleXML solution and can't find one.
hal10001
You don't need to do a case insensitive search. It will always be uppercase. If you are searching an arbitrary node name, just cast it to uppercase using strtoupper().
Dustin Fineout