2014-10-27 97 views
1

我需要一些帮助,使用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.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> 

这里是PHP代码,我使用的固化:

$fp = fopen('data.dat', 'r'); 

$xml = new XMLWriter; 
$xml->openURI('php://output'); 
$xml->setIndent(true); 
$xml->startElement('Airports'); 

while ($line = fgetcsv($fp, 0, ';')) 
{ 
    if ($line[0] == 'AP') 
    { 
     $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'); 
    } 

    if($line[0] == 'RW') 
    { 
     $xml->startElement('Runway', $line[1]); 
     $xml->endElement(); 
    } 

    if($line[0] == 'AP') 
    { 
     $xml->endElement(); 
     $xml->endElement(); 
    } 
} 
$xml->endElement(); 

但是,这是什么东西被创造:

<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> 

有人能指引我走向正确的方向吗?

回答

1

startElement只需要一个参数,在这种情况下您可能需要WriteElement
同时您的机场和跑道的endElement是有点过

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

就像一个魅力;比我想象的要简单。 – Nicholas 2014-10-27 03:39:35