...

View Full Version : Javascript reading XML



GreenFanta
09-22-2011, 05:44 PM
Hi there!

I'm having a little bit of trouble, I've read through tutorials on parsing XML to Javascript and such, but the XML file I'm working with is very different to the ones the tutorial showed me.
Normally it'd be like


<here>
<there></there>
</here>


But the one I'm wanting to work in looks like this:


<rowset name="names" columns="name">
<row name="this">
<rowset name="fun" columns="name">
<row name="that">
</rowset>
</row>
</rowset>


To make it more clear, my question to you is:
How can I parse <row name="that"> where only we only have <row name="this">

Old Pedant
09-22-2011, 08:05 PM
Ummm...are you *SURE* of that XML??

It doesn't look well-formed, from here.

You have a <rowset> nested inside of another <rowset>.

On top of that, you have two <row>s but only one </row>.

GreenFanta
09-22-2011, 08:23 PM
<rowset name="alliances" key="allianceID" columns="name,shortName,allianceID,executorCorpID,memberCount,startDate">
<row name="CORE." shortName="CORE." allianceID="223369706" executorCorpID="777041021" memberCount="129" startDate="2006-04-30 02:23:00">
<rowset name="memberCorporations" key="corporationID" columns="corporationID,startDate">
<row corporationID="643897705" startDate="2006-10-23 00:14:00"/>
</rowset>
</row>
</rowset>

There is the XML file, the one </row> does not get closed.

Old Pedant
09-22-2011, 08:41 PM
Ahhh...you missed the /> of the inner <row... in your first post.

*NOW* it's legal.

Well, you *can* simply do something like


var rows = xml.getElementsByTagName("row");
for ( var r = 0; r < rows.length; ++r )
{
var row = rows[r];
if ( row.name == "CORE." )
{
... do something ...
}
}

or similar. That is, process all elements of a given tag name looking for a match on characteristics.

You can also do it by looking for <row> inside of <rowset> inside of <row>, etc.

So not sure how complex this XML grows to. If no worse than what you showed, should be fine.

GreenFanta
09-22-2011, 08:44 PM
The only issue is that it changes and I only want to use the entries inside of the second row tags and nothing else.

Old Pedant
09-22-2011, 08:49 PM
You mean you need to find this one:

<row corporationID="643897705" startDate="2006-10-23 00:14:00"/>

So does that one *always* have a corporationID property?

If so, you could simply look for that property existing.

var rows = xml.getElementsByTagName("row");
for ( var r = 0; r < rows.length; ++r )
{
var row = rows[r];
if ( row.corporationID != null )
{
... do something ...
}
}

No?

GreenFanta
09-22-2011, 08:51 PM
No, under each row entry the corporation ID is unique.

Old Pedant
09-22-2011, 09:07 PM
Read again: You just want to see if the property *exists*. If it does, you have a valid row. If not, it's a different kind of <row>.

That is, a <row> such as

<row name="CORE." shortName="CORE." allianceID="223369706" executorCorpID="777041021" memberCount="129" startDate="2006-04-30 02:23:00">
doesn't have a corporationID property.

No? Am I way off base???

GreenFanta
09-22-2011, 09:16 PM
No, I want to be able to check all of the corporationID's under a specific row (the one that contains shortname and such)

Old Pedant
09-22-2011, 09:31 PM
Oh...then it's easy.

First use the technique I showed to find the specific <row> (that is, look for shortname property, look for particular value for shortname if desired).

Then simply do:


var subrows = row.getElementsByTagName("row");
for ( var s = 0; s < subrows.length; ++s )
{
var subrow = subrows[s];
... process one subrow...
}

GreenFanta
09-22-2011, 09:48 PM
So I did this:


var subrows = xmlDoc.getElementsByTagName("row");
for ( var s = 0; s < subrows.length; ++s )
{
var subrow = subrows[s];
document.write(subrow.name + '<br>');
}

And I am being returned with "undefined"

Old Pedant
09-22-2011, 10:00 PM
??? mystifies me.

Show more code? How'd you create xmlDoc??

GreenFanta
09-22-2011, 10:19 PM
Will do some research and then let you know.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum