2

I get data from XML. I need to check data and if data is null I must hide this.

How can I check?

  <script>
   downloadUrl("gxml.php", function(data) {
       var xml = data.responseXML;
       var markers = xml.documentElement.getElementsByTagName("marker");
       for (var i = 0; i < markers.length; i++) {
         var oid = markers[i].getAttribute("objectid");
         var status = markers[i].getAttribute("status");
         var title = markers[i].getAttribute("title");
         var volume = markers[i].getAttribute("volume");
         var kwh = markers[i].getAttribute("kwh");
         var puser = markers[i].getAttribute("puser");
         var ucomp = markers[i].getAttribute("ucomplate");
         var udate = markers[i].getAttribute("udate");
         var suser = markers[i].getAttribute("suser");
         var scomp = markers[i].getAttribute("scomplate");
         var sdate = markers[i].getAttribute("sdate");
            var type = markers[i].getAttribute("type");
         var point = new google.maps.LatLng(
             parseFloat(markers[i].getAttribute("lat")),
             parseFloat(markers[i].getAttribute("lng")));
         var html = "ObjectID:"  + oid + "  <br/> Title:"  + title +"  <br/>Status:" + status + "  <br/>Volume:" + volume + "  <br/> KWh:" + kwh + "  <br/>User:" + puser + "  <br/> User Date:" + udate + "  <br/> User Complate :" + ucomp + "  <br/>Super user:" + suser + "  <br/> S .User Date:" + sdate + "  <br/> S. User Complate :" + scomp + "<br/> <a href=\"edit.php\">Add/Edit</a>";

</script>

// this data i get from xml. i`m need to chek this null or not null

2 Answers 2

3

You can simply do a if (!myVariable) check on any given variable. If that returns true (that is to say if the code enters the if body, then the data is null. What I would do is instead of doing this:

var oid = markers[i].getAttribute("objectid");
// ...
..."ObjectID:"  + oid + "

Include your title in the variable, like this:

var oid = markers[i].getAttribute("objectid");
if (oid) {
    oid = "ObjectID: " + oid;
}

and that way you can add oid directly to your string. If the value is null, adding it will have no effect because the variable will be empty. If there was a value, then the title will be appended and doing a + oid will append the title and value at the same time.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! Please consider marking this as the response if it helped you out :)
0
if ( !foo ) {
    // foo is not set, it is null.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.