Re: XML DOM
| From: | Benjamin Hawkes-Lewis | Date: | Thu, 22 Jul 2010 06:45:07 +0000 |
| Subject: | Re: XML DOM | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-307082@lists.php.net to get a copy of this message | ||
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