Re: XML DOM

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21 Jul 2010, at 19:32, Ben Miller wrote:
> Problem:
> 
> If street2 (or any other field) has no value, PHP is outputting the XML node
> as <street2 />, which is producing a JS error when I try to call:
> 
> <script type="text/javascript">..
> 
> street2 = x[i].getElementsByTagName("street2")[0].childNodes[0].nodeValue;
> 
>                ...</script>   (Because the requested node has no
> .nodeValue, I'm assuming???)
> 
> 
> 
> Question:
> 
> Can I tell PHP to output the XML node as <street2></street2> instead of
> <street2 /> so that JS sees that the object has a value, but the value is ""
> (blank)?

"<street2>foo</street2>" is syntax that means a "street2" element with one child node: text node "foo".

"<street2></street2>" and "<street2 />" are different syntax that mean the same thing: a "street2" element with no child nodes.

Your JS property lookup fails because the "street2" element has no child nodes. Rather than changing your XML serialization to have some sort of opaque meaning, why not do one of the following in your JS?

   1. Test to see if the element in question has any child nodes before attempting to access the first one:

   if (x[i].getElementsByTagName("street2")[0].childNodes.length > 0) ...

   2. Use the "textContent" and "innerText" properties to get the text of all/any child nodes.

--
Benjamin Hawkes-Lewis
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux