javascript - 使用 Javascript 解析 XML 对象数组中的数据

标签 javascript jquery xml

我的 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/

相关文章:

javascript - onclick =""与事件处理程序

android - 在android中绘制自定义形状

python - 在Python中将XML元素转换为字符串,然后将字符串转换为XML文件

javascript - Google Apps 脚本 - 将带有样式的 HTML 转换为 PDF 并附加到电子邮件 - 从 HTML 创建 PDF Blob

javascript - 切换/隐藏 div JQuery

javascript - 换行符在 window.open().document 中不起作用

javascript - JQuery:图像 src 变量清理

jquery ("foo bar") vs jquery ("foo").find ("bar")

javascript - 带有 json 格式数据的 AJAX 脚本不起作用

java.lang.NoClassDefFoundError : Could not initialize class org. springframework.beans.CachedIntrospectionResults 问题