XML DOM
| From: | Ben Miller | Date: | Wed, 21 Jul 2010 18:32:31 +0000 |
| Subject: | XML DOM | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-307072@lists.php.net to get a copy of this message | ||
Hi,
I'm using PHP to output an XML file for use with Google Maps - the XML file
containing data for each location.
$dom = new DOMDocument("1.0");
$locations = $dom->createElement("locations");
$dom->appendChild($locations); // append $node as a child of $dom (I
think???)
for($i=1;$i<=$number_of_locations;$i++) {
$row = mysql_fetch_assoc($query);
$location = $dom->createElement("location");
// Create a new element "location"
$locations->appendChild($location); //
Append "location" as a child of "locations" (the document shell)
$name =
$dom->createElement("name",htmlentities($row['location_name']));
$location->appendChild($name);
.. And so on
Ultimately, I want to have an XML output that looks like (which I'm ALMOST
getting, but.):
<?xml version="1.0" ?>
<locations>
<location>
<name>Some company name</name>
<street>12345 Some Street</street>
<street2>Some Apt Number</street2>
<city>Some City</city>
<state>CA</state>
<zip>90210</zip>
</location>
</locations>
Then I'm using JS to feed each location into Google Maps, creating a sidebar
entry and marker for each.
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)?
Many thanks in advance for any help.
Ben