SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)

    Dealing with empty XML nodes (Prototype)

    I have the following XML data:

    Code:
    <data>
    <wp_hours_on>24.00</wp_hours_on>
    <wp_tubing_press>30</wp_tubing_press>
    <wp_casing_press>0</wp_casing_press>
    <wp_volume>43</wp_volume>
    <wp_current_rate>38</wp_current_rate>
    <wp_comments/>
    <sc_volume>42</sc_volume>
    <sc_current_rate>44</sc_current_rate>
    <fm_volume>0</fm_volume>
    <c_hours_on>24.00</c_hours_on>
    <c_suction>19</c_suction>
    <c_discharge>0</c_discharge>
    <c_comments>SIBU @ 3:00 p.m</c_comments>
    <ot589_top_ft>8</ot589_top_ft>
    <ot589_top_in>8</ot589_top_in>
    <ot589_bt_ft>0</ot589_bt_ft>
    <ot589_bt_in>7</ot589_bt_in>
    <ot589_prod>0.00</ot589_prod>
    </data>
    Currently, this data is refreshed using an AJAX call from Prototype. The issue that comes up is that sometimes, different nodes will have empty data. I need to check each node on every refresh to clear out a form field value if that node is empty.

    Any ideas on how to do this? I've tried checking for an empty string, but that doesn't work. Now I'm thinking that I should just check to see if the node exists but I have my doubts that that will work either.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Hartmann View Post
    I've tried checking for an empty string, but that doesn't work.
    What do you find that the empty node has instead?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What do you find that the empty node has instead?
    Paul, essentially the node is empty. I need a way to go through the nodes and ignore the empty ones. As soon as the Javascript hits the empty node it stops running.

    So when I run this:

    Code:
    $('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
    and the <wp_comments> element is empty (<wp_comments />) I need to skip that element and move on to the next one?

    I tried this as well:

    Code:
    cNode = transport.responseXML.getElementsByTagName('wp_comments')[0];
    if(cNode.ChildNodes.length) {
    $('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
    }
    and it does not seem to work.
    Thanks for the help!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Hartmann View Post
    and the <wp_comments> element is empty (<wp_comments />) I need to skip that element and move on to the next one?
    So would it work appropriately if you checked if there are any comments?

    Code javascript:
    var comments = transport.responseXML.getElementsByTagName('wp_comments');
    if (comments) {
        $('wp_comments').value = comments[0].firstChild.nodeValue;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    So would it work appropriately if you checked if there are any comments?

    Code javascript:
    var comments = transport.responseXML.getElementsByTagName('wp_comments');
    if (comments) {
        $('wp_comments').value = comments[0].firstChild.nodeValue;
    }
    I did:

    Code javascript:
    var comments = transport.responseXML.getElementsByTagName('wp_comments');
    if(comments) {
         alert("true!");
    }

    And even if the node is empty, the alert pops up.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Hartmann View Post
    And even if the node is empty, the alert pops up.
    Check its length instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Check its length instead.
    Great idea! Tried this:

    Code javascript:
    var comments = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
    		if(comments.length > 0)
    		{
    			alert(comments.length);
    			$('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
    		}

    It correctly returns the length if there is one.

    Now, I need to apply this to every node I have. Any ideas on consolidating the "if" statements?

    Thanks!

  8. #8
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Playing with this some more, it appears that the empty node still breaks the XML structure, even if I ignore it in the JavaScript.

    I may just need to modify the script that generates the XML to not include blank items. So if my value is null in the database, just don't generate a tag for that item.


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
  •