我試圖從 xml 檔案中提取資料并將其顯示在表格中,但結果并未如我所愿。我希望每個<tag>人都有<string>每個<destinationSymbols>串列中的串列。但就目前而言,它只<string>為每個回傳第一個<destinationSymbols>
<?xml version="1.0"?>
<ArrayOfHighwayRoutingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HighwayRoutingData>
<tag>I80</tag>
<destinationSymbols>
<string>SFO</string>
<string>OAK</string>
<string>EMR</string>
<string>ELC</string>
<string>RIC</string>
<string>SPB</string>
</destinationSymbols>
</HighwayRoutingData>
<HighwayRoutingData>
<tag>SR24</tag>
<destinationSymbols>
<string>OAK</string>
<string>ORI</string>
<string>LFY</string>
<string>WCR</string>
</destinationSymbols>
</HighwayRoutingData>
<HighwayRoutingData>
<tag>US101</tag>
<destinationSymbols>
<string>SFO</string>
<string>SSC</string>
<string>MIL</string>
<string>PAO</string>
</destinationSymbols>
</HighwayRoutingData>
</ArrayOfHighwayRoutingData>
<?php
$file = "RouteSymbol.xml";
if (file_exists($file)) {
$orders = simplexml_load_file($file,"SimpleXMLElement", LIBXML_NOERROR | LIBXML_ERR_NONE) or die("Error: Cannot create object");
echo "<table border='1'>";
foreach ($orders->xpath("//HighwayRoutingData") as $routingPoints){
$tag=(string)$routingPoints->tag;
//$string=(string)$routingPoints->string;
echo "<tr>";
echo "<td>".$tag."</td>";
echo "</tr>";
foreach($orders->xpath("//destinationSymbols") as $symbols){
$string=(string)$symbols->string;
echo "<tr>";
echo "<td>".$string."</td>";
echo "</tr>";
/*foreach ($orders->xpath("//destinationSymbols". $tag . """) as $symbol){
$string=(string)$symbol->string;
echo "<tr>";
echo "<td>".$string."</td>";
//echo "</tr>";*/
}
}
echo "</table>";
}else{
echo "Invalid request!";
}
預期輸出
-------
| I80 |
=======
| SFO |
-------
| OAK |
-------
| EMR |
-------
| ELC |
-------
| RIC |
=======
-------
| SR24 |
=======
| OAK |
-------
| ORI |
-------
| LFY |
-------
| WCR |
=======
-------
| US101 |
=======
| SFO |
-------
| SSC |
-------
| MIL |
-------
| PAO |
=======
uj5u.com熱心網友回復:
嘗試以下方法:
$orders = simplexml_load_string($data);
echo "<table border='1'>";
foreach ($orders->xpath(".//HighwayRoutingData") as $routingPoints){
$tag=(string)$routingPoints->tag;
echo "<tr><td><b>{$tag}</b></td>";
foreach($routingPoints->xpath(".//destinationSymbols//string") as $symbol){
$x = (string)$symbol;
echo "<tr><td>{$x}</td></tr>";
}
echo "</tr>";
}
echo "</table>";
輸出應該是您的預期輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/385830.html
上一篇:將XML轉換為資料框
