简体   繁体   中英

Parse data from XML objects array using Javascript

My XML object looks like this. I want to know how to look through it and fetch startdate and end date data. Later I want to compare and group appointments with same date together. I am good with JSON but I don't know how to manipulate XML. I am trying

$(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>

You need to use :eq() to get the appointment at the given index. $(xmlData).find("appointment")[0] will give an element reference which is not a jQuery object so .find() will give an error

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM