0

我正在做一个项目,并且由于限制而苦苦挣扎。我通常会用 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>
......
4

1 回答 1

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>
于 2013-05-15T08:28:22.543 回答