簡體   English   中英

使用 XML 填充表

[英]Using XML to populate table

我試圖從 xml 文件中提取數據並將其顯示在表格中,但結果並未如我預期的那樣出現。 我希望每個<tag>都有來自每個<destinationSymbols>列表的<string>列表。 但就目前而言,它只返回每個<destinationSymbols>的第一個<string> >

<?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!";
        } 

預期 output

+-------+
| I80   |
+=======+
| SFO   |
+-------+
| OAK   |
+-------+
| EMR   |
+-------+
| ELC   |
+-------+
| RIC   |
+=======+
+-------+
| SR24  |
+=======+
| OAK   |
+-------+
| ORI   |
+-------+
| LFY   |
+-------+
| WCR   |
+=======+
+-------+
| US101 |
+=======+
| SFO   |
+-------+
| SSC   |
+-------+
| MIL   |
+-------+
| PAO   |
+=======+

嘗試以下方式:

$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>";

output 應該是您預期的 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM