Hal Abelson
Hal Abelson

Reputation: 89

how to expire XML node on specific date using js?

I need to expire XML node in a specific date. I added expire date in to XML node. But I have no idea how to automatically remove the node, when it expire using JavaScript. If anyone have some idea, please help.

Here is the code.

contact.xml

<?xml version="1.0" encoding="UTF-8"?>
<contact>
    <person expires="19/09/2018">
        <name>Bob Jones</name>
        <phone>(02)44 42 45 63</phone>
    </person>
    <person expires="21/09/2018">
        <name>Gary Williams</name>
        <phone>(02)44 41 87 56</phone>
    </person>

</contact>

    $(document).ready(function(){
    $.ajax({
        type:"GET",
        url:"contact.xml",
        dataType:"xml",
        success:showdata
    });
});

function showdata(xml){
    xml = $(xml).children();
    $(xml).children().each(function () {                  
        let name = $(this).find("name").text();
        let phone =$(this).find("phone").text();
        
        let html = `<div class="col-md-4">
                    <div class="thumbnail">
                      <p>${name}</p>
                      <p>${phone}</p>
                    </div>
                    </div>`;


       $("#test").append(html);
    });
}
<div class="row" id="test"></div>

Plunker

Upvotes: 0

Views: 67

Answers (1)

SH K
SH K

Reputation: 112

How about this?

function showdata(xml){
    xml = $(xml).children();
    $(xml).children().each(function () {     

        let expireArray = $(this).attr('expires').split('/');
        const expireDate = `${expireArray[2]}${expireArray[1]}${expireArray[0]}`;
        const now = new Date(),
            nowDate = `${now.getFullYear()}${(now.getMonth()+1) <10 ? '0'+(now.getMonth()+1): (now.getMonth()+1)}${now.getDate()}`;

        if (nowDate > expireDate) {
            return;
        }

        let name = $(this).find("name").text();
        let phone =$(this).find("phone").text();

        let html = `<div class="col-md-4">
                    <div class="thumbnail">
                      <p>${name}</p>
                      <p>${phone}</p>
                    </div>
                    </div>`;


       $("#test").append(html);
    });
}

Upvotes: 1

Related Questions