Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Общие вопросы > xml dom


Автор: DeUDero 14.6.2010, 23:43
Подскажите пожалуйста как вывести из файлика x.xml_ часть эксемела, например 
Код

<OriginDestinationOption SectorSequence="1">

<FlightSegment DepartureDateTime="2010-06-28T20:04:00" ArrivalDateTime="2010-06-28T22:03:00" RPH="1" FlightNumber="2560" ResBookDesigCode="L" NumberInParty="1" E_TicketEligibility="Eligible">
<DepartureAirport LocationCode="MIA">Miami International, FL, US</DepartureAirport>
<ArrivalAirport LocationCode="ATL">Atlanta, GA, US</ArrivalAirport>
<OperatingAirline Code="DL">Delta Air Lines</OperatingAirline>
<Equipment AirEquipType="M88">MC DONNELL DOUGLAS MD-88 JET</Equipment>
<MarketingAirline Code="DL">Delta Air Lines</MarketingAirline>

<TPA_Extensions>
<CabinType Cabin="Economy"/>
<JourneyTotalDuration>01:59</JourneyTotalDuration>
</TPA_Extensions>
</FlightSegment>
</OriginDestinationOption>




Код

<?php
$xml='... здесь содержимое x.xml_';
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
foreach ($xml->sResponse->DOTA->PricedItineraries->PricedItinerary->AirItinerary->OriginDestinationOptions->OriginDestinationOption->FlightSegment as $item) {    
echo "<h2>". $item->DepartureAirport. "</h2>";
}
?>


Спасибо smile 

Автор: The_Aleksey 18.6.2010, 13:12
когда-то ковырялся с xml, но потом забросил. Вот тестовый пример, который я написал для освоения:
Код

$xml=utf8_encode('<?xml version="1.0" encoding="UTF-8"?>
<ManyElements>
   <element id="1" attr1="атрибут1" attr2="атрибут2">
      <child>параметр1</child>
      <child>параметр2</child>
      <child>параметр3</child>
   </element>
   <element id="2" attr1="атрибут3" attr2="атрибут4">
      <child>параметр4</child>
      <child>параметр5</child>
      <child>параметр6</child>
      <child>параметр6.1</child>
      <child>параметр6.2</child>
   </element>
   <element id="3" attr1="атрибут5" attr2="атрибут6">
      <child>параметр7</child>
      <child>параметр8</child>
      <child>параметр9</child>
   </element>
</ManyElements>
');

я делал в виде строки, можно подключать файл xml, и далее обработка:

Код

$dom=new domDocument("1.0", "UTF-8");
$dom->loadXML($xml);
$xpc=new DOMXPath($dom);
$result=$xpc->query('//*[@id="2"]');
$element=$result->item(0);
$child=$element->getElementsByTagName("child");
echo 'count elements: '.$child->length."<br>";
for($i=0; $i<$child->length; $i++)
{
   $output=$child->item($i);
   echo utf8_decode($dom->saveXML($output))."<br>";
}
$attr=$element->attributes;
echo 'count attributes: '.$attr->length."<br>";

foreach($attr as $i=>$node)
{
   echo "Атрибут ".$i.": ";
   echo utf8_decode($node->nodeName."=".$node->nodeValue)."<br>";
}


может быть поможет

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)