1

I have a XML data in this format:

<data>
<item type='String'>ABC</item>
<item type='Date'>
<Year>2015</Year>
<Month>May</Month>
<Day>24</Day>
</item>
</data>

I am parsing in this format:

var obj = someVariable.record[0].column[0].getText(); //obj

How can I parse the date 2015/May/24?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
Xender
  • 33
  • 5

1 Answers1

0

In pure JavaScript you can do this as follows:

var xmlv = document.getElementsByTagName("data")[0];
var date = xmlv.querySelector('item[type="date"]');
var year = date.querySelector('Year').innerHTML;
var month = date.querySelector('Month').innerHTML;
var day = date.querySelector('Day').innerHTML;

var parsedDate = year + '/' + month + '/' + day;

Note that this uses the querySelector function which will not work in older browsers but will be fine in IE8+.

Pure JS version (as above) https://jsfiddle.net/n0xamj6L/

jQuery version (neater and more compatible) https://jsfiddle.net/t4pr8oyu/

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
  • Yes, you're getting me right, I want to parse date from XML. I can't use jquery, is there any way to parse it in javascript. And your jsfiddle code is not giving the output. – Xender Jun 24 '15 at 09:47
  • Since jQuery is written in JavaScript it follows that anything in jQuery can be refactored into pure JS (just longer and messier..). I have done this for you and updated the answer. The fiddle works fine for me in Chrome on PC and Android, FireFox and IE11. Perhaps your alerts are suppressed or JS off? Depending on what you mean by "parse" - this fiddle extracts the values in XML and concatenates them to your format in `var parsedDate`. It then alerts the result. – 08Dc91wk Jun 25 '15 at 07:29