Lilia:
1. You need to check if building has any department
//get department
var deptName = null;
if (markers[i].hasChildNodes())
{
alert("for marker (building) " + i + " there is a
child node
");
alert(markers[i].childNodes[0].textContent);
deptName = markers[i].childNodes[0].textContent;
}
else
{
alert("for marker " + i + " there are no child nodes
(departments)");
}
2. Check if building has more than one department
As this is general XML parsing question check out other resources on
XML Parsing, such as
http://www.devx.com/xml/Article/11866/1954
Thanks, Radina
On Apr 7, 9:07 am, Lilia <[email protected]> wrote:
> I have what seems to be the exact same problem as Bern, but it isn't
> resolved by Davide's advice.
>
> I've followed this
> demo:http://code.google.com/apis/maps/articles/phpsqlajax_v3.html
>
> My attempt here:http://pr107m.psur.utk.edu/~lilia/campusmap/help/
>
> I am trying to get at the contents of the "department" element in the XML
> from
> genxml-building-depts.php<http://pr107m.psur.utk.edu/%7Elilia/campusmap/help/genxml-buildings-d...>(php
> shown below):
> // building SQL
> $Bquery = "SELECT building_name, building_id, building_code,
> building_infoLat, building_infoLng, building_type FROM building ORDER BY
> building_name";
>
> $Bresult = mysql_query($Bquery);
> if (!$Bresult) {
> die('Invalid Bquery: ' . mysql_error());
> }
>
> //fetch building data
> while ($Brow = mysql_fetch_array($Bresult, MYSQL_ASSOC))
> {
>
> //for each building
> foreach ($Brow as $key => $value)
> {
>
> //echo out building element & building name
> if ($key=="building_name")
> {
> // ADD TO XML DOCUMENT NODE
> $item =
> $dom->createElement("building");
> $root->appendChild($item);
>
> $item->setAttribute("building_name",parseToXML($value));
> //create text node
> //$Btext =
> $dom->createTextNode(parseToXML($value));
> //$item->appendChild($Btext);
> }
> //give building element attributes
> if ($key=="building_id")
> {
>
> $item->setAttribute("building_id",$value);
> }
> if ($key=="building_code")
> {
>
> $item->setAttribute("building_code",$value);
> }
> if ($key=="building_infoLat")
> {
>
> $item->setAttribute("building_infoLat",$value);
> }
> if ($key=="building_infoLng")
> {
>
> $item->setAttribute("building_infoLng",$value);
> }
> if ($key=="building_type")
> {
>
> $item->setAttribute("building_type",$value);
> //if you're echoing the building_type,
> grab these depts while you're at it
> $Dquery = 'SELECT * FROM departments
> WHERE departments_buildingID = '.$Brow['building_id'];
>
> $Dresult = mysql_query($Dquery);
> if (!$Dresult) {
> die('Invalid Dquery: ' .
> mysql_error());
> }
> //fetch dept array
> while ($Drow =
> mysql_fetch_array($Dresult, MYSQL_ASSOC))
> {
> //echo out department
> info
> foreach ($Drow as $key2
> => $value2)
> {
>
> //echo out
> department element & department name
> if
> ($key2=="departments_name")
> {
> // add
> child node
> $dept
> = $dom->createElement("department");
>
> $item->appendChild($dept);
>
> //create text node
> $Dtext
> = $dom->createTextNode($value2);
>
> $dept->appendChild($Dtext);
> }
> //give
> department element attributes
> if
> ($key2=="departments_buildingID")
> {
>
> $dept->setAttribute("departments_buildingID",$value2);
> }
> if
> ($key2=="departments_room")
> {
>
> $dept->setAttribute("departments_room",$value2);
> }
> if
> ($key2=="departments_url")
> {
>
> $dept->setAttribute("departments_url",$value2);
> }
> if
> ($key2=="departments_phone")
> {
>
> $dept->setAttribute("departments_phone",$value2);
> }
> if
> ($key2=="departments_email")
> {
>
> $dept->setAttribute("departments_email",$value2);
> }
>
> }
> }
> }
>
> }
>
> }
> echo $dom->saveXML();
> ?>
>
> Sample of XML:
> <building building_name="Aconda Court " building_id="3" building_code="AC"
> building_infoLat="35.9571980000" building_infoLng="-83.9299680000"
> building_type="building">
> <department departments_buildingID="3" departments_room="104"
> departments_url="http://www.math.utk.edu/" departments_phone="(865)
> 974-2461" departments_email="[email protected]">Mathematics</department>
> </building>
>
> Like Bern, I'm declaring a variable that attempts to get the first child of
> "building" and I had it structured the same:
> var deptName = markers[i].childNodes[0].nodeValue;
> But Firebug says markers[i].childNodes[0] is undefined. This makes sense to
> me because when I uncomment these lines:
> //$Btext = $dom->createTextNode(parseToXML($value));
> //$item->appendChild($Btext);
> from
> genxml-buildings-depts.php<http://pr107m.psur.utk.edu/%7Elilia/campusmap/help/genxml-buildings-d...>,
> markers[i].childNodes[0].nodeValue spits out the building name. But I don't
> need that, so I leave them commented.
>
> Then, I adjusted my code according to Davide's advice:
> var deptName = markers[i].childNodes[1].textContent;
> But again, Firebug says that markers[i].childNodes[1] is undefined.
>
> I've tried using debuggers in other browsers, but they haven't been able to
> provide any more information to help me troubleshoot.
>
> I tried adjusting my code to match the example that Bern uses (it's still
> commented out inhttp://pr107m.psur.utk.edu/~lilia/campusmap/help/), but I
> get the same message in FireBug: markers[i].childNodes[1] is undefined.
>
> To be honest, I have no clue what I'm doing wrong. From what I've read and
> the instructions I've followed, I think I'm doing it right? Any, any help
> would be very much appreciated. I apologize in advance if my explanation is
> convoluted, I'm just trying to provide a thorough description of what I've
> tried.
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.