Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts

    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">

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    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 08:40 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    The only issue is that it changes and I only want to use the entries inside of the second row tags and nothing else.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    No, under each row entry the corporation ID is unique.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    No, I want to be able to check all of the corporationID's under a specific row (the one that contains shortname and such)

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    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.

  • #11
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    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"

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    ??? 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.

  • #13
    New Coder
    Join Date
    Aug 2011
    Posts
    46
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Will do some research and then let you know.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •