我有以下XML:
<CruiseCharts Copyright="(C) Alsek Technologies, Inc." Exported="2014-01-10T18:34:01" ImagesUrlRoot="http://081198621a967d166a1f-e081145dd5e1f0f3f186009058a70a37.r71.cf1.rackcdn.com/">
<Itinerary TotalFullDays="5" CruisingFullDays="5" StartLocationName="Tampa, Florida" EndLocationName="Tampa, Florida" DistributorName="Carnival" DistributorID="1" SmallMapFilename="166d7d841ae1de8315960a3f3d1e4dce.jpg" LargeMapFilename="564003962db32f0b21d8151ff99c0285.jpg" LargeMapHtmlMap="<map name="map1"><area href="#" onClick="return false" shape="rect" coords="197,18,201,22" title="Tampa, Florida"/><area href="#" onClick="return false" shape="rect" coords="167,13,195,20" title="Tampa, Florida"/><area href="#" onClick="return false" shape="rect" coords="94,189,98,193" title="Cozumel, Mexico"/><area href="#" onClick="return false" shape="rect" coords="57,184,93,191" title="Cozumel, Mexico"/><area href="#" onClick="return false" shape="rect" coords="222,217,226,221" title="George Town, Grand Cayman, Cayman Islands, United Kingdom"/><area href="#" onClick="return false" shape="rect" coords="228,211,282,219" title="George Town, Grand Cayman, Cayman Islands, United Kingdom"/></map>" DistributorLogoFilename="c1b9b02a38e72f5abf793979f5549c77.gif">
<ItineraryItem CalendarDayOffset="0" LocationName="Tampa, Florida, United States" DepartureTime="16:00:00" />
<ItineraryItem CalendarDayOffset="1" LocationName="At Sea" />
<ItineraryItem CalendarDayOffset="2" LocationName="Cozumel, Mexico" ArrivalTime="07:00:00" DepartureTime="15:00:00" />
<ItineraryItem CalendarDayOffset="3" LocationName="George Town, Grand Cayman, Cayman Islands, United Kingdom" ArrivalTime="10:00:00" DepartureTime="18:00:00" />
<ItineraryItem CalendarDayOffset="4" LocationName="At Sea" />
<ItineraryItem CalendarDayOffset="5" LocationName="Tampa, Florida, United States" ArrivalTime="08:00:00" />
<Departure DepartureDate="2014-01-25" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835048" ShipID="51" OperatorID="1" OperatorDepartureCode="20140125PA05" />
<Departure DepartureDate="2014-11-15" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835049" ShipID="51" OperatorID="1" OperatorDepartureCode="20141115PA05" />
<Departure DepartureDate="2014-11-29" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835050" ShipID="51" OperatorID="1" OperatorDepartureCode="20141129PA05" />
<Departure DepartureDate="2014-12-13" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835051" ShipID="51" OperatorID="1" OperatorDepartureCode="20141213PA05" />
<Departure DepartureDate="2015-01-10" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835052" ShipID="51" OperatorID="1" OperatorDepartureCode="20150110PA05" />
<Departure DepartureDate="2015-02-07" ShipName="Carnival Paradise" OperatorName="Carnival" DepartureID="37835053" ShipID="51" OperatorID="1" OperatorDepartureCode="20150207PA05" />
</Itinerary>
<Itinerary TotalFullDays="7" CruisingFullDays="7" StartLocationName="Miami, Florida" EndLocationName="Miami, Florida" DistributorName="Carnival" DistributorID="1" SmallMapFilename="9eb6e4f1f6ba2e6e174e97492f5a7e8e.jpg" LargeMapFilename="8a508e8f9049ff5088e875de2c598364.jpg" LargeMapHtmlMap="<map name="map1"><area href="#" onClick="return false" shape="rect" coords="234,18,238,22" title="Miami, Florida"/><area href="#" onClick="return false" shape="rect" coords="209,13,233,20" title="Miami, Florida"/><area href="#" onClick="return false" shape="rect" coords="93,129,97,133" title="Cozumel, Mexico"/><area href="#" onClick="return false" shape="rect" coords="55,123,91,130" title="Cozumel, Mexico"/><area href="#" onClick="return false" shape="rect" coords="209,154,213,158" title="George Town, Grand Cayman, Cayman Islands, United Kingdom"/><area href="#" onClick="return false" shape="rect" coords="215,148,269,156" title="George Town, Grand Cayman, Cayman Islands, United Kingdom"/><area href="#" onClick="return false" shape="rect" coords="77,166,81,170" title="Costa Maya, Mexico"/><area href="#" onClick="return false" shape="rect" coords="82,160,174,168" title="Costa Maya, Mexico"/><area href="#" onClick="return false" shape="rect" coords="102,216,106,220" title="Mahogany Bay, Roat&#225;n Island, Honduras"/><area href="#" onClick="return false" shape="rect" coords="75,222,133,230" title="Mahogany Bay, Roat&#225;n Island, Honduras"/></map>" DistributorLogoFilename="c1b9b02a38e72f5abf793979f5549c77.gif">
<ItineraryItem CalendarDayOffset="0" LocationName="Miami, Florida, United States" DepartureTime="16:00:00" />
<ItineraryItem CalendarDayOffset="1" LocationName="At Sea" />
<ItineraryItem CalendarDayOffset="2" LocationName="Cozumel, Mexico" ArrivalTime="08:00:00" DepartureTime="17:00:00" />
<ItineraryItem CalendarDayOffset="3" LocationName="Costa Maya, Mexico" ArrivalTime="08:00:00" DepartureTime="16:00:00" />
<ItineraryItem CalendarDayOffset="4" LocationName="Mahogany Bay, Roatan Island, Honduras" ArrivalTime="08:00:00" DepartureTime="15:00:00" />
<ItineraryItem CalendarDayOffset="5" LocationName="George Town, Grand Cayman, Cayman Islands, United Kingdom" ArrivalTime="10:00:00" DepartureTime="18:00:00" />
<ItineraryItem CalendarDayOffset="6" LocationName="At Sea" />
<ItineraryItem CalendarDayOffset="7" LocationName="Miami, Florida, United States" ArrivalTime="08:00:00" />
<Departure DepartureDate="2015-01-04" ShipName="Carnival Splendor" OperatorName="Carnival" DepartureID="37827160" ShipID="40026" OperatorID="1" OperatorDepartureCode="20150104SL07" />
<Departure DepartureDate="2015-02-08" ShipName="Carnival Splendor" OperatorName="Carnival" DepartureID="37835054" ShipID="40026" OperatorID="1" OperatorDepartureCode="20150208SL07" />
</Itinerary>
</CruiseCharts>
我正在解析它,但我得到的结果如下
# text =
Itinerary =
# text =
empty values in tag
我必须在PHP中解析这个xml。我如何解析它并在每次迭代中获取值,格式如下
TotalFullDays="5" CruisingFullDays="5" StartLocationName="Tampa, Florida"
...每个XML标记。
行程,行程项目,离境标签。
我想以这种格式显示数据
编辑:这是我目前的代码:
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("alsek_cruisecharts.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
答案 0 :(得分:0)
我不确定你想要渲染它,但这是解析它的一种方法。将其变成一个对象数组。
try {
$xmlDoc = new DOMDocument();
$xmlDoc->load('file.xml');
if($xmlDoc===false)
{ throw new Exception('File not found.'); }
$root = $xmlDoc->documentElement;
$charts = array();
foreach($root->getElementsByTagName('Itinerary') as $itinerary) {
$chart = array();
# get attributes
foreach($itinerary->attributes as $attribute)
{ $chart[$attribute->name] = $attribute->value; }
# get items
$chart['Itinerary'] = array();
foreach($itinerary->getElementsByTagName('ItineraryItem') as $itineraryItem) {
$item = array();
foreach($itineraryItem->attributes as $attr)
{ $item[$attr->name] = $attr->value; }
$chart['Itinerary'][] = $item;
}
# get departures
$chart['Departures'] = array();
foreach($itinerary->getElementsByTagName('Departure') as $departure) {
$item = array();
foreach($departure->attributes as $attr)
{ $item[$attr->name] = $attr->value; }
$chart['Departures'][] = $item;
}
$charts[] = $chart;
}
# objectify
$charts = json_decode(json_encode($charts));
# print structure
# print_r($charts);
foreach($charts as $chart) {
# use it
}
}
catch (Exception $exc) {
die($exc->getMessage());
}
您可以像这样使用它:
获取第一个DistributorName
的{{1}}:
Itinerary
获取第一个$charts[0]->DistributorName;
中第二个LocationName
的{{1}}:
ItineraryItem
获取第二个Itinerary
中第三个$charts[0]->Itinerary[1]->LocationName;
的{{1}}:
ShipId
如果您想进行一些格式设置,例如将Departure
转换为正确的Itinerary
对象,则需要查看我正在访问$charts[1]->Departures[2]->ShipId;
的{{1}}循环} properties。