Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2011, 04:44 PM   PM User | #1
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
Javascript reading XML

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
Code:
<here>
<there></there>
</here>
But the one I'm wanting to work in looks like this:
Code:
<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">
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 07:05 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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>.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 07:23 PM   PM User | #3
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
Code:
<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.

Last edited by GreenFanta; 09-22-2011 at 07:40 PM..
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 07:41 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahhh...you missed the /> of the inner <row... in your first post.

*NOW* it's legal.

Well, you *can* simply do something like
Code:
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 07:44 PM   PM User | #5
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
The only issue is that it changes and I only want to use the entries inside of the second row tags and nothing else.
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 07:49 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You mean you need to find this one:
Code:
<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.
Code:
var rows = xml.getElementsByTagName("row");
for ( var r = 0; r < rows.length; ++r )
{
    var row = rows[r];
    if ( row.corporationID != null )
    {
       ... do something ...
    }
}
No?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 07:51 PM   PM User | #7
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
No, under each row entry the corporation ID is unique.
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 08:07 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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
Code:
<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???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 08:16 PM   PM User | #9
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
No, I want to be able to check all of the corporationID's under a specific row (the one that contains shortname and such)
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 08:31 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
Code:
var subrows = row.getElementsByTagName("row");
for ( var s = 0; s < subrows.length; ++s )
{
    var subrow = subrows[s];
    ... process one subrow...
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 08:48 PM   PM User | #11
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
So I did this:

Code:
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"
GreenFanta is offline   Reply With Quote
Old 09-22-2011, 09:00 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? mystifies me.

Show more code? How'd you create xmlDoc??
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-22-2011, 09:19 PM   PM User | #13
GreenFanta
New Coder

 
Join Date: Aug 2011
Posts: 46
Thanks: 9
Thanked 2 Times in 2 Posts
GreenFanta is an unknown quantity at this point
Will do some research and then let you know.
GreenFanta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 AM.


Advertisement
Log in to turn off these ads.