使用javascript函数查询两个xml文件

时间:2013-05-15 02:34:38

标签: javascript jquery xml ajax

我正在研究一个项目,并且考虑到这些限制,我正在努力。我通常会用PHP处理这类事情,但项目需要在有限的服务器上运行,我需要完全用javascript来完成。

脚本如下:

1 - 加载XML文件1,找到playState子等于“Running”的节点的“id”(只有一个)。

2 - 使用id,加载XML文件2,并返回ID与步骤1中的值匹配的时间轴的名称。

我完全被难倒了,我尝试过使用jquery选择器,但无法掌握如何实际搜索并返回一个特定值。任何帮助将不胜感激。

XML文件1:

<response>
  <timelineStatus id = "1">
    <playState>Idle</playState>
    <position>P00H00M00.00S</position>
    <onStage>false</onStage>
  </timelineStatus>
  <timelineStatus id = "101">
    <playState>Idle</playState>
    <position>P00H00M00.00S</position>
    <onStage>false</onStage>
  </timelineStatus>
  <timelineStatus id = "102">
    <playState>Running</playState>
    <position>P00H00M00.00S</position>
    <onStage>false</onStage>
  </timelineStatus>
  <timelineStatus id = "103">
    <playState>Idle</playState>
    <position>P00H00M00.00S</position>
    <onStage>false</onStage>
  </timelineStatus>
  <timelineStatus id = "104">
    <playState>Idle</playState>
    <position>P00H00M00.00S</position>
    <onStage>false</onStage>
  </timelineStatus>
......

XML文件2:

<response>
  <timeline id = "1">
    <name>System - Custom Color Base</name>
    <length>P00H00M10.00S</length>
    <timeSource>
      <type>Internal</type>
    </timeSource>
    <timeOffset>P00H00M00.00S</timeOffset>
  </timeline>
  <timeline id = "101">
    <name>Preset - Red</name>
    <length>P00H00M00.00S</length>
    <timeSource>
      <type>Internal</type>
    </timeSource>
    <timeOffset>P00H00M00.00S</timeOffset>
  </timeline>
  <timeline id = "102">
    <name>Preset - Green</name>
    <length>P00H00M00.00S</length>
    <timeSource>
      <type>Internal</type>
    </timeSource>
    <timeOffset>P00H00M00.00S</timeOffset>
  </timeline>
  <timeline id = "103">
    <name>Preset - Blue</name>
    <length>P00H00M00.00S</length>
    <timeSource>
      <type>Internal</type>
    </timeSource>
    <timeOffset>P00H00M00.00S</timeOffset>
  </timeline>
  <timeline id = "104">
    <name>Preset - Magenta</name>
    <length>P00H00M00.00S</length>
    <timeSource>
      <type>Internal</type>
    </timeSource>
    <timeOffset>P00H00M00.00S</timeOffset>
  </timeline>
......

1 个答案:

答案 0 :(得分:0)

以下是使用JavaScript的解决方案的完整详细信息。

<强> xml1.xml

如上所示

<强> xml2.xml

如上所示

<强>解决方案:

<html>
    <head>
        <script>
            function getOutput(){
                var xhr1 = new XMLHttpRequest();
                xhr1.open("GET", "xml1.xml", false);
                xhr1.send(null);
                xml1 = (xhr1.responseXML);

                var xPathRes = xml1.evaluate("//timelineStatus/playState", xml1, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE , null);
                result = new Array();
                var res = xPathRes.iterateNext();

                while (res) {                   
                    var playState = res.textContent;
                    if(playState == "Running")
                    {
                        resParentNode = res.parentNode;
                        var timelinstStatusId = resParentNode.getAttribute("id"); //id="102"

                        var xhr2 = new XMLHttpRequest();
                        xhr2.open("GET", "xml2.xml", false);
                        xhr2.send(null);
                        xml2 = (xhr2.responseXML);
                        console.log(xml2);

                        var xPathRes2 = xml2.evaluate("//timeline[@id='"+timelinstStatusId+"']/name", xml2, null, XPathResult.FIRST_ORDERED_NODE_TYPE , null);
                        result = xPathRes2.singleNodeValue;
                                            console.log(result.textContent); //Preset - Green
                        return result.textContent; //Preset - Green
                        break;
                    }
                    //Iterate through set of Results
                    res = xPathRes.iterateNext();
                }
            }
        </script>
    </head>
<body>
    <a href="javascript:void(0);" onclick="getOutput();">getOutput</a>
</body>
</html>