使用Jquery或javascript进行XML解析

时间:2022-12-01 13:20:12

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.

我是javascript的新手,我正在完成一项任务,根据给定的日期,从月份和总计中获得具有“机器”,“人力”或“软件”的集合的数量。我将不胜感激任何帮助。

<?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>

2 个解决方案

#1


0  

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

您可以使用jQuery来解析XML,还有一个$ .parseXML函数。

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

和这里的演示

#2


0  

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

使用DOMParser API,您可以将XML转换为DOM元素,因此您可以使用getElementById或getElementsByTagName ...方法:

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 });

#1


0  

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

您可以使用jQuery来解析XML,还有一个$ .parseXML函数。

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

和这里的演示

#2


0  

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

使用DOMParser API,您可以将XML转换为DOM元素,因此您可以使用getElementById或getElementsByTagName ...方法:

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 });