Help me understand this - XML parsing

  • Thread starter Google Maps Slovenija
  • Start date
G

Google Maps Slovenija

I'm working with Google Maps API and I have trouble understanding this
problem of mine which is Javascript related. I will be as clear and
short as possible. Any help will be much appreciated!

We have two different XML files:

XML file 1:
---------------
<markers>
<marker name="Hooters" type="restaurant"> </marker>
<marker name="McDonalds" type="restaurant" </marker>
<marker name="Eddys" type="bar" </marker>
</markers>


XML file 2:
--------------
<markers>

<marker>
<name> Hooters </name>
<type> restaurant </type>
</marker>

<marker>
<name> McDonalds</name>
<type> restaurant </type>
</marker>

<marker>
<name> Eddys </name>
<type> bar </type>
</marker>

</markers>

Now I write the Javascript code for parsing XML file 1 or XML file 2:

GDownloadUrl("markers.xml", function(data) {

var xml = GXml.parse(data);
var markers =
xml.documentElement.getElementsByTagName("marker");

followed by a for loop for retrieving info like this:

for (var i = 0; i < markers.length; i++) {
var name = markers.getAttribute("name");
var type = markers.getAttribute("type");

Can someone please explain to me what is the structure of the markers
variable for the XML file 1 and XML file 2 example. I would like to
know how the array markers is structured! Is there any simple code to
view it, so I can understand better how to access its components!


Thanks in advance!


Peter from Slovenija
 
R

RobG

I'm working with Google Maps API and I have trouble understanding this
problem of mine which is Javascript related. I will be as clear and
short as possible. Any help will be much appreciated!

We have two different XML files:

XML file 1:
---------------
<markers>
<marker name="Hooters" type="restaurant"> </marker>
<marker name="McDonalds" type="restaurant" </marker>
<marker name="Eddys" type="bar" </marker>
</markers>

XML file 2:
--------------
<markers>

<marker>
<name> Hooters </name>
<type> restaurant </type>
</marker>

<marker>
<name> McDonalds</name>
<type> restaurant </type>
</marker>

<marker>
<name> Eddys </name>
<type> bar </type>
</marker>

</markers>

Now I write the Javascript code for parsing XML file 1 or XML file 2:

GDownloadUrl("markers.xml", function(data) {

var xml = GXml.parse(data);

I'm not going to comment on the Googl Maps API or your use of it, I'll
leave that to you.

var markers =
xml.documentElement.getElementsByTagName("marker");

followed by a for loop for retrieving info like this:

for (var i = 0; i < markers.length; i++) {
var name = markers.getAttribute("name");
var type = markers.getAttribute("type");


There is no need to declare the variables on every iteration: it
doesn't hurt, it's just not good style. Also, if you may have a large
number of markers, it is more efficient to store the length in a local
variable rather than look it up on each loop. Similarly, if each
marker is to be used for more than a couple of statements, store a
reference to that too:

var marker, name, type;
for (var i=0, len=markers.length; i<len; i++) {
marker = markers;
name = marker.getAttribute("name");
type = marker.getAttribute("type");
}

or, if the order of looping isn't an issue, consider:

var marker, name, type,
i = markers.length;
while (i--) {
marker = markers;
name = marker.getAttribute("name");
type = marker.getAttribute("type");
}

Can someone please explain to me what is the structure of the markers
variable for the XML file 1 and XML file 2 example.

In both cases, markers is a NodeList:

<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-A6C9094 >


A NodeList is a live collection of element references - it's a bit
like an Array, it has a length property and you reference the elements
by index, but it doesn't have any of Array's special methods.

<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-536297177 >

I've given you DOM 2 references - depending on how serious you are
about XML, you may want to look at DOM 3.

I would like to
know how the array markers is structured! Is there any simple code to
view it, so I can understand better how to access its components!

It isn't an array (see above). In XML File 1, name and type are
attributes of the marker elements and can be accessed using say:

alert( marker.name );


In XML File 2, name and type are child elements so you'd need to do
something like (for W3C DOM 2 compliant browsers):

alert( marker.getElementsByTagName('name')[0].textContent );


or, if I understood the GXml reference correctly:

alert( GXml.value(marker.getElementsByTagName('name')[0]) );

<URL: http://www.google.com/apis/maps/documentation/reference.html#GXml

Note that you really should check whether
marker.getElementsByTagName('name')[0] returns a DOM object before
trying to access any of its properties.

Whether name and type should be attributes or a properties is a design
decision that needs careful consideration.
 
T

Thomas 'PointedEars' Lahn

RobG said:
[...] Google Maps Slovenija [...]:
for (var i = 0; i < markers.length; i++) {
var name = markers.getAttribute("name");
var type = markers.getAttribute("type");


There is no need to declare the variables on every iteration:


They are not. The declaration, i.e. the variable instantiation, is only
done once, after control enters the execution context, and before execution.
it doesn't hurt,
Exactly.

it's just not good style.

IBTD. I'd rather have the VariableDeclarations for variables used only in
the loop within the loop than somewhere outside of it.


PointedEars
 
D

David Cox

Thomas 'PointedEars' Lahn said:
RobG said:
[...] Google Maps Slovenija [...]:
for (var i = 0; i < markers.length; i++) {
var name = markers.getAttribute("name");
var type = markers.getAttribute("type");


There is no need to declare the variables on every iteration:


They are not. The declaration, i.e. the variable instantiation, is only
done once, after control enters the execution context, and before
execution.
it doesn't hurt,
Exactly.

it's just not good style.

IBTD. I'd rather have the VariableDeclarations for variables used only in
the loop within the loop than somewhere outside of it.


PointedEars


Apologies for stepping in, but the phrasing seems to reinforce the
confusion.
The variable is declared in the loop definition, not within the loop, just
as you earlier stated.
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Does anyone have a date for this quotation? As a beginner I look at the
morass of 'standards' and implementations and despair, and am sorely tempted
into "Ie6, and **** the rest." I am sure TBL would shudder at that, but and
wonder if he still fully endorses that quote..
 
D

David Mark

"Thomas 'PointedEars' Lahn"

Does anyone have a date for this quotation?

Doesn't really matter.

As a beginner I look at the
morass of 'standards' and implementations and despair, and am sorely tempted
into "Ie6, and **** the rest." I am sure TBL would shudder at that, but and

What about IE7? Seriously, you should resist that temptation. As a
beginner, you likely have no idea what IE6+ does differently from
other (better) browsers. Code for all browsers and you will learn.
Code for IE6+ only and you will learn nothing (except how to produce
incompetent scripts.)
wonder if he still fully endorses that quote..

I imagine he does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top