我正在处理此代码
<?php
$xml='<TOURS>
<TOUR>
<Title><![CDATA[Anzac & Egypt Combo Tour]]></Title>
<Code>AE-19</Code>
<Duration>18</Duration>
<Start>Istanbul</Start>
<End>Cairo</End>
<Inclusions>
<![CDATA[<div style="margin: 1px 0px; padding: 1px 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; text-align: justify; line-height: 19px; color: rgb(6, 119, 179);">The tour price cover the following services: <b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">Accommodation</b>; 5, 4 and 3 star hotels </div>]]>
</Inclusions>
<DEP DepartureCode="AN-17" Starts="04/19/2015" GBP="1458" EUR="1724" USD="2350" DISCOUNT="15%" />
<DEP DepartureCode="AN-18" Starts="04/22/2015" GBP="1558" EUR="1784" USD="2550" DISCOUNT="20%" />
<DEP DepartureCode="AN-19" Starts="04/25/2015" GBP="1558" EUR="1784" USD="2550" />
</TOUR>
<TOUR>
<Title><![CDATA[Anzac & Egypt Combo Tour]]></Title>
<Code>AE-19</Code>
<Duration>18</Duration>
<Start>Istanbul</Start>
<End>Cairo</End>
<Inclusions>
<![CDATA[<div style="margin: 1px 0px; padding: 1px 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; text-align: justify; line-height: 19px; color: rgb(6, 119, 179);">The tour price cover the following services: <b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">Accommodation</b>; 5, 4 and 3 star hotels </div>]]>
</Inclusions>
<DEP DepartureCode="AN-17" Starts="04/19/2015" GBP="1458" EUR="1724" USD="2350" DISCOUNT="15%" />
<DEP DepartureCode="AN-18" Starts="04/22/2015" GBP="1558" EUR="1784" USD="2550" DISCOUNT="20%" />
<DEP DepartureCode="AN-19" Starts="04/25/2015" GBP="1558" EUR="1784" USD="2550" />
</TOUR>
</TOURS>';
$tour=simplexml_load_string($xml) or die("Error: Cannot create object");
echo "<h2>".$tour->getName()."</h2><br />";
foreach($tour->children() as $book)
{
echo "Title : ".$book->Title." <br />";
echo "Code : ".$book->Code." <br />";
echo "Duration : ".$book->Duration." <br />";
echo "Inclusions : ".$book->Inclusions." <br />";
echo "MinPrice : ".$book->DEP[0]->." <br />";
echo "<hr/>";
}
?>
我能够解析XML的所有实体但是我不知道如何解析以下的xml代码
<DEP DepartureCode="AN-17" Starts="04/19/2015" GBP="1458" EUR="1724" USD="2350" DISCOUNT="15%" />
<DEP DepartureCode="AN-18" Starts="04/22/2015" GBP="1558" EUR="1784" USD="2550" DISCOUNT="20%" />
<DEP DepartureCode="AN-19" Starts="04/25/2015" GBP="1558" EUR="1784" USD="2550" />
我的输出应该是所有这三个中的平均值
EUR 1724 - 15% = 1465
EUR 1784 - 20% = 1427
EUR 1784 - 0 = 1784
然后总= 1465 + 1427 + 1784 = 4676&lt; - 这是我想解析xml后要显示的内容。
所以整个代码的OUTPUT应该是
TOURS
Title : Anzac & Egypt Combo Tour
Code : AE-19
Duration : 18
Inclusions :
The tour price cover the following services: Accommodation; 5, 4 and 3 star hotels
Min Price : 4676
Title : Anzac & Egypt Combo Tour
Code : AE-19
Duration : 18
Inclusions :
The tour price cover the following services: Accommodation; 5, 4 and 3 star hotels
Min Price : 4676
我现在上网后感到很累,我希望你们帮助我。我非常感谢您的时间和帮助。
谢谢。
答案 0 :(得分:1)
$tour = simplexml_load_string($xml) or die("Error: Cannot create object");
// In real world you will likely need to open a file with fopen, e.g.:
// $fh = fopen('file.csv', 'w');
// Don't forget to close it afterwards with fclose($fh);
$fh = STDOUT;
$delim = ':';
foreach ($tour->children() as $book) {
$min_price = 0;
//foreach ($book->xpath('DEP') as $d) {
foreach ($book->DEP as $d) {
$discount = (int)(string)$d['DISCOUNT'];
$eur = (int)(string)$d['EUR'];
$min_price += $eur * (1 - $discount / 100);
}
fputcsv($fh, ['Title', $book->Title], $delim);
fputcsv($fh, ['Code', $book->Code], $delim);
fputcsv($fh, ['Duration', $book->Duration], $delim);
fputcsv($fh, ['Inclusions', trim(strip_tags($book->Inclusions))], $delim);
fputcsv($fh, ['MinPrice', (int)$min_price], $delim);
}
输出
Title:"Anzac & Egypt Combo Tour"
Code:AE-19
Duration:18
Inclusions:"The tour price cover the following services: Accommodation; 5, 4 and 3 star hotels "
MinPrice:4676
Title:"Anzac & Egypt Combo Tour"
Code:AE-19
Duration:18
Inclusions:"The tour price cover the following services: Accommodation; 5, 4 and 3 star hotels "
MinPrice:4676
迭代$book->DEP
,或在SimpleXmlElement::xpath()
方法的帮助下使用相对XPath表达式'DEP'
。在这种特殊情况下,您可以简单地迭代DEP
属性。我已经展示了XPath版本的完整性。当您需要根据复杂的逻辑选择节点时,XPath非常有用。
属性作为数组项进行访问。使用类型转换获得的数值:
SimpleXmlElement::__toString
方法允许获取元素的字符串表示形式; (int)
将前导数字转换为整数类型,例如(int)"20%" === 20
。