jQuery.parseXML() JQ Home  <<  Utilities  <<  jQuery.parseXML()

Parsed data to XML document creation.

Description

The jQuery.parseXML() jQuery General utility method, returns an XML document created from the parsed string.

Shorthand version $.parseXML()

  • Creates a valid XML Document from the native parsing function of the browser. The XML document can then be passed to jQuery for creation of a jQuery object.

Syntax

Signature Description
jQuery.parseXML( data )Return an XML document created from the parsed string.

Parameters

Parameter Description Type
dataA well-formed XML string to be parsed.String

Return

An XML document.

jQuery.parseXML( data ) Example Utilities  <<  Top

Return an XML document created from the parsed string.

In the example below when we press the button the first time we parse some well formed XML to a variable and then convert the variable to a jQuery object. After this we iterate over the xml nodes to print out some fictitious diary entries.


$(function(){
  $('#btn21').one('click', function(){
    var xml = "<xml version='1.0'>" +
    "<week date='01/01/2012 '><day activity='shopping '><loc>London</loc></day></week>" +
    "<week date='02/01/2012 '><day activity='driving '><loc>Cardiff</loc></day></week>" +
    "<week date='03/01/2012 '><day activity='eating '><loc>Glasgow</loc></day></week></xml>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc );
     // Print Diary details
    $($xml).find('week').each(function() {
      $('#div17').append($(this).attr('date'));
      $('#div17').append($(this).find('day').attr('activity'));
      $('#div17').append($(this).find('loc').text() + "<br>");
    });
  });
});

div17. Some initial text.

Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 9 - jQuery General Utilities