jquery - Query two xml files with javascript function -


i working on project , struggling given restrictions. handle sort of thing php project needs run on server limited, need accomplish entirely javascript.

script follows:

1 - load xml file 1, find "id" of node has playstate child equal "running" (there one).

2 - using id, load xml file 2, , return name of timeline id matches value step 1.

i stumped, i've tried using jquery selectors can't handle on how search , return 1 specific value. appreciated.

xml file 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 file 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> ...... 

here full details of solution javascript.

xml1.xml

as shown above

xml2.xml

as shown above

solution:

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

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -