我的 XML 对象如下所示。我想知道如何查看它并获取开始日期和结束日期数据。稍后我想比较相同日期的约会并将其分组在一起。我擅长 JSON,但不知道如何操作 XML。我正在努力
$(xmlData).find("appointment")[0]['startdate']
$(xmlData).find("appointment")[0].find('startdate')
<appointment>
<appointmentid>2015032303585487505373377</appointmentid>
<startdate>20150323000000</startdate>
<enddate>20150323080000</enddate>
<description>Outside Office Hours</description>
<location>
<locationid></locationid>
<description></description>
</location>
<resource>
<resourceid>00000009</resourceid>
<description>John Doe</description>
<status>C</status>
</resource>
<colours>
<primary>eee</primary>
<secondary>eee</secondary>
<border>eee</border>
</colours>
</appointment>
<appointment>
<appointmentid>2015032303585487505273377</appointmentid>
<startdate>20150323000000</startdate>
<enddate>20150323080000</enddate>
<description>Outside Office Hours 1</description>
<location>
<locationid></locationid>
<description></description>
</location>
<resource>
<resourceid>00000009AJ</resourceid>
<description>Dr Nikhil</description>
<status>C</status>
</resource>
<colours>
<primary>eee</primary>
<secondary>eee</secondary>
<border>eee</border>
</colours>
</appointment>
<appointment>
<appointmentid>2015032303585487505273377</appointmentid>
<startdate>20150323000000</startdate>
<enddate>20150323080000</enddate>
<description>Outside Office Hours</description>
<location>
<locationid></locationid>
<description></description>
</location>
<resource>
<resourceid>0000000</resourceid>
<description>Dr Seema</description>
<status>C</status>
</resource>
<colours>
<primary>eee</primary>
<secondary>eee</secondary>
<border>eee</border>
</colours>
</appointment>
最佳答案
您需要使用 :eq() 在给定索引处获取约会
。 $(xmlData).find("appointment")[0]
将给出一个不是 jQuery 对象的元素引用,因此 .find()
将给出错误
$(xmlData).find("appointment:eq(0) startdate").text()
关于javascript - 使用 Javascript 解析 XML 对象数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206461/