PHP将分隔文本文件转换为XML

PHP将分隔文本文件转换为XML,php,xml,Php,Xml,我需要一些帮助,使用PHP将分隔文本文件转换为可用的XML文件 文本文件本身包括数百个机场及其受人尊敬的跑道;以下是一个小样本: AP;KLAX;LOS ANGELES INTL;33.942495;-118.408070 RW;06L;33.949109;-118.431156 RW;06R;33.946853;-118.434239 RW;07L;33.935825;-118.419336 RW;07R;33.933645;-118.419014 RW;24L;33.950189;-118.

我需要一些帮助,使用PHP将分隔文本文件转换为可用的XML文件

文本文件本身包括数百个机场及其受人尊敬的跑道;以下是一个小样本:

AP;KLAX;LOS ANGELES INTL;33.942495;-118.408070
RW;06L;33.949109;-118.431156
RW;06R;33.946853;-118.434239
RW;07L;33.935825;-118.419336
RW;07R;33.933645;-118.419014
RW;24L;33.950189;-118.401664
RW;24R;33.952100;-118.401945
RW;25L;33.937359;-118.382708
RW;25R;33.939553;-118.382906
AP;KSFO;SAN FRANCISCO INTL;37.618817;-122.375428
RW;01L;37.609453;-122.381897
RW;01R;37.607689;-122.380139
RW;10L;37.628739;-122.393392
RW;10R;37.626289;-122.393106
RW;19L;37.627342;-122.367111
RW;19R;37.626481;-122.370608
RW;28L;37.612095;-122.359264
RW;28R;37.613917;-122.358056
基本上,我需要将其解析为以下格式的可用XML文件:

<Airports>
    <Airport ID="KLAX" Name="LOS ANGELES INTL">
        <Location Lat="33.942495" Lon="-118.408070" />
        <Runways>
            <Runway>06L</Runway>
            <Runway>06R</Runway>
            <Runway>07L</Runway>
            <Runway>07R</Runway>
            <Runway>24L</Runway>
            <Runway>24R</Runway>
            <Runway>25L</Runway>
            <Runway>25R</Runway>
        </Runways>
    </Airport>
    <Airport ID="KSFO" Name="SAN FRANCISCO INTL">
        <Location Lat="37.618817" Lon="-122.375428" />
        <Runways>
            <Runway>01L</Runway>
            <Runway>01R</Runway>
            <Runway>10L</Runway>
            <Runway>10R</Runway>
            <Runway>19L</Runway>
            <Runway>19R</Runway>
            <Runway>28L</Runway>
            <Runway>28R</Runway>
        </Runways>
    </Airport>
</Airports>
但这是正在创造的:

<Airports>
 <Airport ID="KLAX" Name="LOS ANGELES INTL">
  <Location Lat="33.942495" Lon="-118.408070"/>
  <Runways/>
 </Airport>
</Airports>
<Airport ID="KSFO" Name="SAN FRANCISCO INTL">
 <Location Lat="37.618817" Lon="-122.375428"/>
 <Runways/>
</Airport>

有人能给我指出正确的方向吗?

只接受一个参数,在这种情况下可能需要。
另外,你对机场和跑道的喜爱也有点不太好

$first = true;
while ($line = fgetcsv($fp, 0, ';'))
{
    if ($line[0] == 'AP')
    {
        if (!first){
        // close the runway and airport tags
            $xml->endElement();
            $xml->endElement();
        }
        $xml->startElement('Airport');
        $xml->writeAttribute('ID', $line[1]);
        $xml->writeAttribute('Name', trim($line[2]));

        // Location
        $xml->startElement('Location');
        $xml->writeAttribute('Lat', $line[3]);
        $xml->writeAttribute('Lon', $line[4]);
        $xml->endElement();

        $xml->startElement('Runways');
        $first = false;
    }

    if($line[0] == 'RW')
    {
        $xml->writeElement('Runway', $line[1]);
    }
}
// close all open tags
while ($xml->endElement() !== false) { continue; }

工作就像一种魅力;一定比我想象的要简单。
$first = true;
while ($line = fgetcsv($fp, 0, ';'))
{
    if ($line[0] == 'AP')
    {
        if (!first){
        // close the runway and airport tags
            $xml->endElement();
            $xml->endElement();
        }
        $xml->startElement('Airport');
        $xml->writeAttribute('ID', $line[1]);
        $xml->writeAttribute('Name', trim($line[2]));

        // Location
        $xml->startElement('Location');
        $xml->writeAttribute('Lat', $line[3]);
        $xml->writeAttribute('Lon', $line[4]);
        $xml->endElement();

        $xml->startElement('Runways');
        $first = false;
    }

    if($line[0] == 'RW')
    {
        $xml->writeElement('Runway', $line[1]);
    }
}
// close all open tags
while ($xml->endElement() !== false) { continue; }