0

How to get the root element attribute from an XML file URL and pass as a bootstrap table data?

Javascript has to be used to do the above task. URl: http://xxxxxx/y.xml

For example:

<xxx-yyyyy zzzzzzz="2016-11-03T06:34:59+02:00">

I want to get the date in this root element.

Please help

sindhu
  • 21
  • 4

1 Answers1

1
  • First you need to do a AJAX call to fetch the data from your xml file.
  • Second inside your success method you will receive the XML data, you need to parse this data using $.parseXML() and then convert it into a jquery object using $(yourXML) after that its simple as working on your DOM objects.
  • You can read the attribute using the .attr() function.

Below is the example code, (here is a fiddle which demonstrates working with xml data in jquery)

$(function(){  // alternate syntax for $(document).ready(function(){..
    $.ajax({
      type: "GET" ,
      url: "http://xxxxxx/y.xml" ,
      dataType: "xml" ,               // make sure to set the dataType to xml
      success: function(xml) {

        var xmlDoc = $.parseXML(xml); //Parse the givn XML
        var $xml = $(xmlDoc);         // convert XML into jquery object
        var creationDate = $xml.find("ris-metadata").attr('creation');
        alert(creationDate);          // this will give you the required data.

        // this will update the second column data of the first row.
        $('#yourTableId tbody td:eq(1)').text(creationDate); 
       }
    });
});
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59