Reputation: 1225
I am getting a response xml when i invoke a rest service in my html page. Response xml looks like :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<sys:systems xmlns:sys="http://tempuri.org">
<system>
<ID>System1</ID>
<ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime>
<ScheduledEndDateTime></ScheduledEndDateTime>
<operations>
<operation>
<OperationID>0010</OperationID>
<Status>Running</Status>
<ID>10003814</ID>
</operation>
</operations>
</system>
</sys:systems>
Then i am trying to parse the above response using jQuery and for that i am doing something like :
$(xmlDoc).find('sys\\:systems\\system\\ID,ID').text();
Upon doing alert of above statement i get the ID Value but i get both the ID value instead of getting only one ID value.
So what i am trying to fetch is ID=System1 but i am getting both System1 as well as 10003814.
How can i get only System1 as ID? Looking forward to your solutions. Thanks in advance.
Upvotes: 2
Views: 113
Reputation: 29683
Alternatively you iterate through each <system>
when there are multiple <system>
$(xml).find("system").each(function(){
var unitData=$(this).find("ID:first").text()
alert(unitData);
});
var xml='<sys:systems xmlns:sys="http://tempuri.org"><system><ID>System1</ID><ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime><ScheduledEndDateTime></ScheduledEndDateTime><operations><operation><OperationID>0010</OperationID><Status>Running</Status><ID>10003814</ID></operation></operations></system><system><ID>System1</ID><ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime><ScheduledEndDateTime></ScheduledEndDateTime><operations><operation><OperationID>0010</OperationID><Status>Running</Status><ID>10003814</ID></operation></operations></system></sys:systems>';
$(xml).find("system").each(function(){
var unitData=$(this).find("ID:first").text()
alert(unitData);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1
Reputation: 337627
You can target the ID
node you want directly using the descendant selector. Try this:
var id = $(xmlDoc).find('system > ID').text();
Working example:
var xmlDoc = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><sys:systems xmlns:sys="http://tempuri.org"><system><ID>System1</ID><ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime><ScheduledEndDateTime></ScheduledEndDateTime><operations><operation><OperationID>0010</OperationID><Status>Running</Status><ID>10003814</ID></operation></operations></system></sys:systems>'
var id = $(xmlDoc).find('system > ID').text();
alert(id);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1