使用Ajax或PHP调用URl

时间:2014-09-23 12:14:56

标签: php html ajax xml web-services

我必须调用一个返回XML的Web URL

<Account account="ihs" timezone="GMT+05:30">
  <Description>The Indian Heights School</Description>
  <Device id="dl1pc2814">
    <Description>DL1PC2814</Description>
    <EventData device="dl1pc2814">
      <Timestamp epoch="1411466654">2014/09/23 15:34:14 GMT+05:30</Timestamp>
      <StatusCode code="0xF020">Location</StatusCode>
      <GPSPoint age="0">28.56150,77.05312</GPSPoint>
      <Speed units="km/h">0.0</Speed>
      <Heading degrees="0.0">N</Heading>
      <Altitude units="meters">0</Altitude>
      <Odometer units="Km">12002.1</Odometer>
      <Geozone index="0">tihs</Geozone>
      <Address>The Indian Heights School</Address>
      <City/>
      <PostalCode/>
      <DigitalInputMask>0x0000000000000000</DigitalInputMask>
      <DriverID/>
      <DriverMessage/>
      <EngineRPM>0</EngineRPM>
      <EngineHours>0.0</EngineHours>
      <VehicleBatteryVolts>0.0</VehicleBatteryVolts>
      <EngineCoolantLevel units="percent">0.0</EngineCoolantLevel>
      <EngineCoolantTemperature units="C"/>
      <EngineFuelUsed units="Liter"/>
    </EventData>
  </Device>
</Account>

我想要<GPSPoint>个节点值 和 网址是

Link

我尝试了这个,但它无法正常工作

  <script>
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.open("GET", "http://ent.davts.in:5885/events/dev.xml?a=ihs&u=&p=archana2013&d=dl1pc2814&l=1&at=true", false);

  xmlhttp.send();
  alert(xmlhttp.readyState);
  xmlDoc = xmlhttp.responseXML;

  document.write("<table id='myTable2' border='1'  >");

  var x = xmlDoc.getElementsByTagName("Description");

  for (i = 0; i < x.length; i++)
  {
       document.write("<tr><td>");
       document.write(x[i].getElementsByTagName("Timestamp")[0].childNodes[0].nodeValue);
       document.write("</td><td>");
       document.write(x[i].getElementsByTagName("StatusCode")[0].childNodes[0].nodeValue);
       document.write("</td><td>");
       document.write(x[i].getElementsByTagName("GPSPoint")[0].childNodes[0].nodeValue);
       document.write("</td><td>");
       document.write(x[i].getElementsByTagName("Speed")[0].childNodes[0].nodeValue);
       document.write("</td><tr>");

   }
   document.write("</table>");
   </script>

我使用的是ajax,但我的readystate代码为1,状态代码为0。 实际上我是java开发人员但客户端不是java服务器所以他想要javascript,ajax和php代码中的应用程序 请提前帮助我谢谢

1 个答案:

答案 0 :(得分:0)

jsfiddle链接:JS Fiddle

var text = "Your xml text";
parser=new DOMParser();
var xmlDoc = parser.parseFromString(text,"text/xml");
var x = xmlDoc.getElementsByTagName("EventData");

  for (i = 0; i < x.length; i++)
  {
      console.log("Timestamp = "+x[i].getElementsByTagName("Timestamp")[0].childNodes[0].nodeValue);
console.log("StatusCode = "+x[i].getElementsByTagName("StatusCode")[0].childNodes[0].nodeValue);
console.log("GPSPoint = "+x[i].getElementsByTagName("GPSPoint")[0].childNodes[0].nodeValue);
console.log("Speed = "+x[i].getElementsByTagName("Speed")[0].childNodes[0].nodeValue);
  }