2014-09-23 205 views
0

我要調用一個Web URL這是返回XML呼叫使用Ajax或PHP

<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>節點值 和 URL網址是

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代碼 應用程序請幫助我,在提前感謝

回答

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); 
    } 
+0

我要調用一個跨域URL不是一個XML文件 – 2014-09-24 04:49:03