how to remove the duplicate xml notes content using jquery

110 Views Asked by At

I am trying to remove the duplicate xml notes using jquery, can any one help?

My xml:

<Accounts>
  <Account>
    <Company>Company 1</Company>
    <AccountNo>12345</AccountNo>
    <CCY>EUR</CCY>
    <Link>Link</Link>
    <StatusIMG>Approved</StatusIMG>
  </Account>
  <Account>
    <Company>Company 1</Company>
    <AccountNo>12345</AccountNo>
    <CCY>EUR</CCY>
    <Link>Link</Link>
    <StatusIMG>Approved</StatusIMG>
  </Account>
  <Account>
    <Company>Company 1</Company>
    <AccountNo>12345</AccountNo>
    <CCY>EUR</CCY>
    <Link>Link</Link>
    <StatusIMG>Approved</StatusIMG>
  </Account>
</Accounts>

JS:

$(document).ready(function () {

var LatestAccounturl = "scripts/jqwidgets/sampledata/latestAccountUpdates.xml";

var LatestAccountsource =
{
    datatype: "xml",
    mtype: 'GET',
    datafields: [
        { name: 'Company', type: 'String'},
        { name: 'AccountNo', type: 'int' },
        { name: 'CCY', type: 'String' },
        { name: 'Link', type: 'bool' },
        { name: 'StatusIMG', type: 'String' }
    ],      
    root: "Accounts",
    record: "Account",
    url: LatestAccounturl       
};


var xml = $(xml);
$('Accounts > Company', xml).each(function() {
    alert($(this).text());
});

});

I want to check if the <company> node having the same value (duplicated) then i need to remove the duplicated item, for this first step I am trying to get the xml node text value, which is not working in my alert:( any solution or better approach ?

1

There are 1 best solutions below

7
On BEST ANSWER

The problem is the select: Accounts > Company tries to select direct children, but in the xml, Company is not a direct child of Accounts.

correct would be

$('Accounts Company', xml).each(function() {
    alert($(this).text());
});

without the >, or

$('Accounts > Account', xml).each(function() {
    alert($(this).find("Company").text());
});

which would be the preffered method, since this will hold the full account....now you can check for the company already existing and use $(this).remove() to delete the node, in case its already present.