Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have a xml file which i am taking form webserice like this :-
XML
<person>
      <id>5397080</id>
      <name>ravi</name>
      <age>25</age>
      <address>AV</address>
      <phone>855-241-1478</phone>
      <business>test</business>
      <email>ghj@test.com</email>
    </person>
  <person>
    <id>5397081</id>
    <name>kumar</name>
    <age>35</age>
    <address>AV</address>
    <phone>855-241-4845</phone>
    <business>own</business>
    <email>wer@test.com</email>
  </person>
  </profile>

from this xml file i want get the id if name = "ravi". i want to do this using jqvascript or jquery.

thanks in advance
Posted
Updated 17-Apr-13 20:07pm
v2

1 solution

Hello,

You can use code similar to one shown below to perform the search.
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
var xmlData = '<profiles><person><id>5397080</id><name>ravi</name><age>25</age>' +
              '<address>AV</address><phone>855-241-1478</phone>' +
              '<business>test</business><email>ghj@test.com</email>' +
              '</person><person><id>5397081</id><name>kumar</name>' +
              '<age>35</age><address>AV</address><phone>855-241-4845</phone>' +
              '<business>own</business> <email>wer@test.com</email>' +
              '</person></profiles>';
$(document).ready(function() {
    var xmlDoc = $.parseXML( xmlData );
    var nod = $(xmlDoc).find("person name:contains('ravi')");
    var pNod = nod.parent();
	var iNod = $(pNod).find('id');
	alert(iNod.text());

});
</script>
</head>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

Regards,
 
Share this answer
 
Comments
[no name] 18-Apr-13 3:35am    
i m getting below error :-
Microsoft JScript runtime error: Object doesn't support property or method 'parseXML'
so tried like this :-
<script type="text/javascript">
var xmlData = '<profiles><person><id>5397080<name>ravi<age>25' +
'
AV
<phone>855-241-1478</phone>' +
'<business>test<email>ghj@test.com' +
'</person><person><id>5397081<name>kumar' +
'<age>35
AV
<phone>855-241-4845</phone>' +
'<business>own <email>wer@test.com' +
'</person></profiles>';
$(document).ready(function () {
//var xmlDoc = $.parseXML(xmlData);
var xmlDoc = xmlData;
var nod = $(xmlDoc).find("person name:contains('ravi')");
var pNod = nod.parent();
var iNod = $(pNod).find('id');
alert(iNod.text());

});
</script>

but still data is coming empty here..
Prasad Khandekar 18-Apr-13 3:50am    
Are you using correct JQuery version (1.9.1)? Which browser are you using for testing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900