Fjern noder fra XML dokument
Hmm jeg har et problem, jeg vil fjerne en node og dens subnoder fra et XML dokument men kan ikke få det til at virke....Det nemmeste er nok at se koden og kommentaren i koden...
Først min funktion til der gerne skulle finde frem til den node jeg vil fjerne (plus dens subnoder)
// 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)
{
// Slet min flight node med subnoder her!!!
}
}
showXML(xmlDoc);
}
Her kommer lige mit XML dokument:
<?xml version="1.0" encoding="UTF-8"?>
<flights>
<flight>
<agency_name>Supersaver</agency_name>
<agency_id>1</agency_id>
<flight_company>KLM</flight_company>
<out_departure_date_time>2006-05-12 10:00:00</out_departure_date_time>
<out_arrival_date_time>2006-05-12 11:15:00</out_arrival_date_time>
<home_departure_date_time>2006-05-12 11:45:00</home_departure_date_time>
<home_arrival_date_time>2006-05-12 13:00:00</home_arrival_date_time>
<flight_number_out>KLM2123</flight_number_out>
<flight_number_home>KLM2223</flight_number_home>
<departure_iata>BLL</departure_iata>
<departure_name>Billund</departure_name>
<destination_iata>LON</destination_iata>
<destination_name>London</destination_name>
<booking_url>http://www.supersaver.dk</booking_url>
<price_total>2478</price_total>
<price_person>1239</price_person>
<currency>DKK</currency>
</flight>
<flight>
<agency_name>Seat24</agency_name>
<agency_id>2</agency_id>
<flight_company>KLM</flight_company>
<out_departure_date_time>2006-05-12 10:00:00</out_departure_date_time>
<out_arrival_date_time>2006-05-12 11:15:00</out_arrival_date_time>
<home_departure_date_time>2006-05-12 11:45:00</home_departure_date_time>
<home_arrival_date_time>2006-05-12 13:00:00</home_arrival_date_time>
<flight_number_out>KLM2123</flight_number_out>
<flight_number_home>KLM2223</flight_number_home>
<departure_iata>BLL</departure_iata>
<departure_name>Billund</departure_name>
<destination_iata>LON</destination_iata>
<destination_name>London</destination_name>
<booking_url>http://www.seat24.dk</booking_url>
<price_total>2500</price_total>
<price_person>1250</price_person>
<currency>DKK</currency>
</flight>
</flights>
Jeg formoder at det er noget med removeChild men jeg er gået kold i hvordan...