XML I Javascript igen igen
Jeg har stadig problemer med noget XML. Helt konkret 2 problemer:1. I Firefox kommer der dobbelt resultater ud i det jeg prøver at udskrive min XML som en tabel, prøv at kig på http://www.noproblem.dk/xml/show_result.html i IE og Firefox
2. Jeg kan ikke få min fjern agency til at virke, den fjerner noget man den fjerner ikke det hele og ikke det den skal
Min XML kan ses på http://www.noproblem.dk/xml/result.xml
Koden ses her:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Test af findrejsen.dk</title>
<script language="JavaScript">
//<![CDATA[
var xmlDoc = null;
var oXmlHttp = null;
// Get element by id
function el(id)
{
return document.getElementById(id);
}
// Create instance of xmlHttp
function createXmlHttp()
{
if(typeof XMLHttpRequest != "undefined")
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
for(var i=0; i<aVersions.length; i++)
{
try { return new ActiveXObject(aVersions[i]); } catch(oError) { }
}
}
throw new Error("XMLHttp object could not be created");
}
// Get data state change
function getDataStateChange(func)
{
if (oXmlHttp.readyState==4)
{
if (oXmlHttp.status==200)
{
func(oXmlHttp);
}
}
}
// Load data
function load(url,resultFunction)
{
if (!oXmlHttp)
{
oXmlHttp = createXmlHttp();
}
else if (oXmlHttp.readyState != 0)
{
oXmlHttp.abort();
}
oXmlHttp.open("GET",url,true)
oXmlHttp.onreadystatechange=function() { getDataStateChange(resultFunction); }
oXmlHttp.send(null);
}
function test()
{
xmlDoc = oXmlHttp.responseXML;
showXML(xmlDoc);
}
// Remove agency
function removeAgency(agencyId)
{
var agencyName, agencyId, flightCompany, priceTotal, outDepartureDateTime, outArrivalDateTime;
var homeDepartureDateTime, homeArrivalDateTime, flightNumberOut, flightNumberHome, departureIata, departureName
var destinationIata, destinationName, bookingUrl, priceTotal, pricePerson, currency
var nodes, len, newXmlDoc, removeFlight;
removeFlight = false;
nodes = xmlDoc.documentElement.childNodes;
len = nodes.length;
for (i=0; i<len; i++)
{
for (ii=0; ii<nodes.item(i).childNodes.length; ii++)
{
nodeText = nodes.item(i).childNodes[ii].text;
if (nodes.item(i).childNodes[ii].tagName == "agency_id")
{
if (nodeText == agencyId)
{
removeFlight = true;
}
}
}
if (removeFlight)
{
for (ii=0; ii<nodes.item(i).childNodes.length; ii++)
{
removeFlight = false;
nodes.item(i).removeChild(nodes.item(i).childNodes[ii]);
}
}
}
showXML(xmlDoc);
}
// Get inner text
function getInnerText(node)
{
if (typeof node.textContent != 'undefined')
{
return node.textContent;
}
else if (typeof node.innerText != 'undefined')
{
return node.innerText;
}
else if (typeof node.text != 'undefined')
{
return node.text;
}
}
// Show the xml document as html
function showXML(xmlDoc)
{
var agencyName, agencyId, flightCompany, priceTotal, outDepartureDateTime, outArrivalDateTime;
var homeDepartureDateTime, homeArrivalDateTime, flightNumberOut, flightNumberHome, departureIata, departureName
var destinationIata, destinationName, bookingUrl, priceTotal, pricePerson, currency
var html,nodes,len;
nodes = xmlDoc.documentElement.childNodes;
len = nodes.length;
html = '<table border="1">';
for (i=0; i<len; i++)
{
for (ii=0; ii<nodes.item(i).childNodes.length; ii++)
{
nodeText = getInnerText(nodes.item(i).childNodes[ii]);
switch(nodes.item(i).childNodes[ii].tagName)
{
case 'agency_name':
agencyName = nodeText;
break;
case 'agency_id':
agencyId = nodeText;
break;
case 'flight_company':
flightCompany = nodeText;
break;
case 'out_departure_date_time':
outDepartureDateTime = nodeText;
break;
case 'out_arrival_date_time':
outArrivalDateTime = nodeText;
break;
case 'home_departure_date_time':
homeDepartureDateTime = nodeText;
break;
case 'home_arrival_date_time':
homeArrivalDateTime = nodeText;
break;
case 'flight_number_out':
flightNumberOut = nodeText;
break;
case 'flight_number_home':
flightNumberHome = nodeText;
break;
case 'departure_iata':
departureIata = nodeText;
break;
case 'departure_name':
departureName = nodeText;
break;
case 'destination_iata':
destinationIata = nodeText;
break;
case 'destination_name':
destinationName = nodeText;
break;
case 'booking_url':
bookingUrl = nodeText;
break;
case 'price_total':
priceTotal = nodeText;
break;
case 'price_person':
pricePerson = nodeText;
break;
case 'currency':
currency = nodeText;
break;
}
}
html += "<tr>";
html += "<td>" + agencyName + "</td>";
html += "<td>" + agencyId + "</td>";
html += "<td>" + flightCompany + "</td>";
html += "<td>" + priceTotal + "</td>";
html += "<td>" + outDepartureDateTime + "</td>";
html += "<td>" + outArrivalDateTime + "</td>";
html += "<td>" + homeDepartureDateTime + "</td>";
html += "<td>" + homeArrivalDateTime + "</td>";
html += "<td>" + flightNumberOut + "</td>";
html += "<td>" + flightNumberHome + "</td>";
html += "<td>" + departureIata + "</td>";
html += "<td>" + departureName + "</td>";
html += "<td>" + destinationIata + "</td>";
html += "<td>" + destinationName + "</td>";
html += "<td>" + bookingUrl + "</td>";
html += "<td>" + priceTotal + "</td>";
html += "<td>" + pricePerson + "</td>";
html += "<td>" + currency + "</td>";
html += "</tr>";
}
html += "</table>";
el('searchResult').innerHTML = html;
}
//]]>
</script>
</head>
<body onload="load('http://www.noproblem.dk/xml/result.xml',test);">
<a href="java script:removeAgency(2);">Remove agency</a><br/>
<div id="searchResult"></div>
</body>
</html>
Jeg håber at der er en der kan give mig et virkende eksempel, jeg har kæmpet med dette skidt det meste af dagen.