0

I am new to javascript and I am working on a task to get the count of sets that has "machine", "human", or "software" from the month wise and total in any year based on the dates given in . I would appreciate any help provided.

<?xml version="1.0"?>
<resultset>
<set>
    <type>type1</type>
    <name>Patil</name>
    <date>2015-03-21</date>
    <tag>ThisIsALongText-|tag_machine</tag>
    <c_date>2015-01-12</c_date>
</set>
<set>
    <type>type1</type>
    <name>Patil</name>
    <date>2016-06-11</date>
    <tag>ThisIsALongText-|tag_human</tag>
    <c_date>2013-07-17</c_date>
</set>
<set>
    <type>type1</type>
    <name>Patil</name>
    <date>2016-05-21 </date>
    <tag>ThisIsALongText-|tag_software</tag>
    <c_date>2014-11-12</c_date>
</set>
<set>
    <type>type1</type>
    <name>Patil</name>
    <date>2015-12-10</date>
    <tag>ThisIsALongText-|tag_human</tag>
    <c_date>2015-04-12</c_date>
</set>
<set>
    <type>type1</type>
    <name>Patil</name>
    <date>2015-03-21</date>
    <tag>ThisIsALongText-|tag_machine</tag>
    <c_date>2016-02-23</c_date>
</set>

  • What code have you tried? – Makyen Aug 06 '16 at 01:06
  • Either one will do. jQuery is a JavaScript Library. Downside to jQuery is initial load time. You should learn JavaScript first anyways, but jQuery does make it easy to develop and it's in eveyone's cache. – StackSlave Aug 06 '16 at 01:07
  • Possible duplicate? [how-to-parse-xml-using-jquery](http://stackoverflow.com/questions/7228141/how-to-parse-xml-using-jquery) – ooXei1sh Aug 06 '16 at 01:21

2 Answers2

0

With the DOMParser API, you can turn XML into DOM element, and therefore you can use getElementById or getElementsByTagName ... methods :

var parser = new DOMParser();
var doc = parser.parseFromString(yourXML, "text/xml");

EDIT

var parser = new DOMParser();
var doc = parser.parseFromString('<resultset> <set> <type>type1</type> <name>Patil</name> <date>2015-03-21</date> <tag>ThisIsALongText-|tag_machine</tag> <c_date>2015-01-12</c_date> </set> <set> <type>type1</type> <name>Patil</name> <date>2016-06-11</date> <tag>ThisIsALongText-|tag_human</tag> <c_date>2013-07-17</c_date> </set> <set> <type>type1</type> <name>Patil</name> <date>2016-05-21 </date> <tag>ThisIsALongText-|tag_software</tag> <c_date>2014-11-12</c_date> </set> <set> <type>type1</type> <name>Patil</name> <date>2015-12-10</date> <tag>ThisIsALongText-|tag_human</tag> <c_date>2015-04-12</c_date> </set> <set> <type>type1</type> <name>Patil</name> <date>2015-03-21</date> <tag>ThisIsALongText-|tag_machine</tag> <c_date>2016-02-23</c_date> </set>', "text/xml");
var set = doc.getElementsByTagName('set');
var dates = doc.getElementsByTagName('date');
var date1Month = dates[0].innerHTML.split('-')[1];
var date1Year = dates[0].innerHTML.split('-')[0];

var arrDates = [].map.call(dates, function(element) { return element; });

var allDates = arrDates.map(function(date) { return date.innerHTML });
boehm_s
  • 5,254
  • 4
  • 30
  • 44
  • it looks like I have to get the year and month using regex and then the tag also using regex to get started. any code snippet could be useful if u have. – Varun Krishnan Aug 06 '16 at 01:10
0

You can use jQuery to parse XML, there is a $.parseXML function.

Use it like this :

var xml = $.parseXML(myXML);
var tags = $(xml).find('tag');

The variable tags will be an array of all your tags.

More informations here

and a demo here

Rust in Peace
  • 176
  • 2
  • 8