Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading XML embedded in HTML for Google Maps.

Status
Not open for further replies.

deluxmilkman

Programmer
Sep 26, 2007
17
0
0
JP
I`m putting markers on Google Mpas using maps.xml,
and I would like to include maps.xml into HTML to make it one file.

Now, I need to know how to read this XML and process.

my XML in HTML.
Code:
<xml id='xmldata' style='display:none;'> 
<markers>
<marker lat="50.895842" lng="-1.4051" />
</markers>
</xml>

my Javascript for external XML file (I need to change this for embedded XML).
Code:
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(50.895842,-1.4051), 14);
	  map.setUIToDefault();
      map.enableGoogleBar();

      // Read the data from example.xml
      var request = GXmlHttp.create();
      request.open("GET", "maps.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = request.responseXML;
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            // create the marker
            var marker = createMarker(point;
            map.addOverlay(marker);
          }
        }
      }
      request.send(null);

Thank you.
 

I`m trying to write XML in HTML and read it with JavaScript,
but it`s not working.
please help.

my XML in HTML.
Code:
<xml id="xmldata" style='display:none;'>
<markers>
<marker lat="50.895842" lng="-1.4051" html="text"/>
</markers>
</xml>

Javascript trying to read my XML.
Code:
document.getElementById('xmldata');
if(xml.documentElement == null)
xml.documentElement = xml.firstChild;

 var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            var html = markers[i].getAttribute("html");
          

            // create the marker
            var marker = createMarker(point,label,html);
            map.addOverlay(marker);
          }
 
[1] Suppose you wrap the xml "file" structure in a div. (Inline-style should have no effect on the xml part. But if you want to, put an inline style in the wrapping div.)
[tt]
<div>
<xml id='xmldata'>
<markers>
<marker lat="50.895842" lng="-1.4051" />
</markers>
</xml>
</div>
[/tt]
[2] Ie supports xml data-island and will be very much ready to entertain the dom operation, whereas in the moz, you can always parse it to the memory. Suppose in a handler which need lng value, it would be something like this.
[tt]
if (window.ActiveXObject) {
var oxml=document.getElementById("xmldata");
} else if (document.implementation && document.implementation.createDocument) {
var oxml=(new DOMParser).parseFromString(document.getElementById("xmldata").innerHTML,"text/xml");
} else {
var oxml=null;
}
var lng="";
if (!!oxml) {
var lng=parseFloat(oxml.getElementsByTagName("marker")[0].getAttribute("lng"));
}
alert(lng); //for testing
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top