jQuery Data Type How to - Parse XML








Question

We would like to know how to parse XML.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<!--from  w ww .  j av  a 2 s  .  c  om-->
</head>
<body>
<script type='text/javascript'>

var xml = $.parseXML('<r><image><url>URL1 HERE</url><id>ID1 HERE</id></image><image><url>URL2 HERE</url><id>ID2 HERE</id></image></r>');
jQuery(xml).find('image').each(function (i) {
    console.log(jQuery(this).find('url').text())
});

</script>
</body>
</html>

The code above is rendered as follows: