SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast zink's Avatar
    Join Date
    Jul 2006
    Location
    UK
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remote Scripting & XML problem

    I am remote scripting using javascript to an XML gateway, the XML file I get back changes state i.e. either the car registration number exists (and it includes certain fields in the XML) or it doesn't exist (and it doesn't include certain fields).

    When my code tries to display the result I am getting the 'object required' error because I am trying to access an xml element which doesn't exist.

    For example this line would break if the plate didnt exist but work perfectly if it did:
    Code:
    var fuel = response.getElementsByTagName('Fuel')[0].firstChild.data;

    Is there anyway to stop it throwing an error if it doesn't exist and just set a regDoesntExist flag or something?

    This is driving me nuts

    Thanks!

  2. #2
    SitePoint Enthusiast panthro's Avatar
    Join Date
    Dec 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi zink, firstly do you have any control over the server that generates the xml data? if so then you should return a default value (in my experience empty tags also cause issues) that would not be real data in your app i.e.

    valid data: <Fuel>petrol</Fuel>
    no data: <Fuel>0</Fuel>


    Assuming you don't, a simple try & catch can detect the lack of data

    Code:
    try {
       var fuel = response.getElementsByTagName('Fuel')[0].firstChild.data;
    } catch (err) {
       var fuel = false;
    }


    Things to do When Bored

    My List of Classic Kids TV - Includes links to fansites

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anyway to stop it throwing an error if it doesn't exist
    Check if the element exists first:
    Code:
    if(response.getElementsByTagName("RegNumber") )
    {
              var fuel = response.getElementsByTagName('Fuel')[0].firstChild.data;
    }

  4. #4
    SitePoint Enthusiast zink's Avatar
    Join Date
    Jul 2006
    Location
    UK
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Try / Catch blocks work like a dream thanks guys. I forgot you could do this in JS when I think of them I think of old school java.

    thanks again!

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Try / Catch blocks work like a dream thanks guys.
    Not very elegant. Why not write code that works?

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not write code that works?
    I'm gonna make a bumper sticker out of that! That's a good one, 7!

    The coffee is almost ready! Who wants some?


Bookmarks

Posting Permissions

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