使用Javascript从XML对象数组中解析数据

时间:2015-03-23 09:08:12

标签: javascript jquery xml

我的XML对象看起来像这样。我想知道如何查看它并获取startdate和结束日期数据。后来我想将同一日期的约会进行比较和分组。我很擅长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>

1 个答案:

答案 0 :(得分:1)

您需要使用:eq()来获取给定索引处的appointment$(xmlData).find("appointment")[0]将提供一个不是jQuery对象的元素引用,因此.find()将给出错误

$(xmlData).find("appointment:eq(0) startdate").text()