Several Methods of jQuery Processing XML Files

  • 2021-06-28 10:41:25
  • OfStack

XML and HTML are both markup languages, and their grammatical forms are very similar, belonging to the same language system.For HTML analysis, it is very convenient for me to use jQuery. In fact, we can also use jquery to resolve XML, which is also very convenient.

If you have used Java, PHP and other languages to parse XML, I believe you will feel the same as I do, it is very troublesome.When jQuery is used instead to read, analyze and manipulate XML, it gives an unexpected sense of relaxation.Let's take a look at several forms of processing XML files using jQuery.

Resolving XML using the general JavaScript method


var xmlDoc = request.responseXML;
try // Build Markers, if available
{
 var markers = xmlDoc.getElementsByTagName("marker") ;
 for ( var i = 0; i < markers.length ; i++ ) {
  var point = {
   markers[i].getAttribute("lat")),
   markers[i].getAttribute("lng")
  };
 }
} catch(e) {}

Resolving XML using jQuery


$(request.responseXML).find("marker").each(function() {
 var marker = $(this);
 var point = {
  marker.attr("lat"),
  marker.attr("lng")
 };
});

Resolving XML using $.parseXML()

If you have read the XML content into a string, you can analyze it using the $.parseXML() method:


xml = $.parseXML( $('body > pre').text() );

$(xml).find("entry").each(function() {
  var $this = $(this), 
    item = {
      Address1: $this.find("Address1").text(),
      Address2: $this.find("Address2").attr('name')
      
    }
});

This is the whole content of this article, and I hope it will be helpful for you to learn JavaScript program design.


Related articles: