Im trying to pull data from an xml file and display it in a table but the results are not coming out as I intended. I would like is for each <tag>
to have the <string>
listing from each <destinationSymbols>
list. But as it stands right now it only returns the first <string>
for each <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!";
}
Expected output
-------
| I80 |
=======
| SFO |
-------
| OAK |
-------
| EMR |
-------
| ELC |
-------
| RIC |
=======
-------
| SR24 |
=======
| OAK |
-------
| ORI |
-------
| LFY |
-------
| WCR |
=======
-------
| US101 |
=======
| SFO |
-------
| SSC |
-------
| MIL |
-------
| PAO |
=======
CodePudding user response:
Try something along these lines:
$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>";
The output should be your expected output.