I need help displaying an XML file with PHP in table form

68 views Asked by At

I need to grab the contents of an XML file and display it in a table. Since it has various levels I would need it to be a table within a table within a table (Inception style). Now here comes the tricky part, the information that I need are the attributes and its names, but they aren't always present and I don't know the names of the attributes. I have been working trying to use a recursive function using foreach() unsuccessfully. I have been using simplexml_load_file() and then I can print the content but I can't do it in table form. The closest I have gotten to a solution is like this (just an example):

for ($i = 0; $i <= 5 ; $i++){
    foreach($xml2->level_0[$i]->level_1[$j]->attributes() as $a => $b){
        echo $a . ' => ' . $b . '<br/>';
    }

I would greatly appreciate some help since I can't seem to find enough information.

The perfect solution would be a script that runs through the file and can display as a table the information without caring for the name of the tags or the names of the attributes. Below is an example of what the XML file looks like.

<level_0 xmlns="http://www.google.com" EchoToken="185345732" MessageContentCode="1" Target="Production" TimeStamp="2015-06-01T06:40:21" Version="2.001">
    <level_1 ChainCode="HX" HotelCode="HXBASZZ">
        <level_2 BookingLimit="1" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="DD1" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="2" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="0" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="ED0" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="1" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="9" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="ED1" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="3" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="13" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="ED2" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="5" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="3" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="ED3" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="7" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="2" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="ET0" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="9" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="5" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="PD1" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="4" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="8" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="PD2" Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="6" ID_Context="16" />
        </level_2>
        <level_2 BookingLimit="4" BookingLimitMessageType="SetLimit">
            <level_3_1 End="2015-06-06" Fri="true" InvTypeCode="PD3"   Mon="true" Sat="true" Start="2015-06-06" Sun="true" Thur="true" Tue="true" Weds="true" />
            <level_3_2 ID="8" ID_Context="16" />
        </level_2>
    </level_1>
</level_0>
2

There are 2 answers

1
MaggsWeb On BEST ANSWER

OK - try this one then..

$xml = simplexml_load_file($file);

echo '<table>';

foreach($xml->level_1->level_2 as $item){

    $level3 = $item->level_3_1->attributes();

    echo '<tr>';

    foreach($level3 as $name => $value){

        echo "<th>$name</th>";
        echo "<td>$value</td>";

    }

    echo '</tr>';

}

echo '</table>';
1
MaggsWeb On

Try this:

$xml = simplexml_load_file($file);

echo '<table>';

foreach($xml->level_1->level_2 as $item){

    echo '<tr>';

    $End = $item->level_3_1->attributes()->End->__toString();
    echo "<td>$End</td>";

    $InvTypeCode = $item->level_3_1->attributes()->InvTypeCode->__toString();
    echo "<td>$InvTypeCode</td>";

    echo '</tr>';

}

echo '</table>';