PHPを使用してxmlからhtmlテーブルを作成しようとしています。XML は次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<Device name="3" alias="THIS">
<RecCommand name="CLICK">
<GlobalState name="Light Is Off">
<Conditions>
<Device name="3">
<State name="LEVEL">
<Test name="LT">50</Test>
</State>
</Device>
</Conditions>
<Commands>
<Device name="3">
<Command name="ON">
</Command>
</Device>
</Commands>
</GlobalState>
<GlobalState name="Light Is On">
<Conditions>
<Device name="3">
<State name="LEVEL">
<Test name="GTE">50</Test>
</State>
</Device>
</Conditions>
<Commands>
<Device name="3">
<Command name="OFF">
</Command>
</Device>
</Commands>
</GlobalState>
</RecCommand>
</Device>
そして、私が現在取り組んでいる機能:
function foo($parent) {
if ($parent->children()->count() > 0) {
$j = '<tr><td>' . $parent->getName();
foreach($parent->attributes() as $attrName => $attrValue) {
$j .= ' ' . $attrName. '=' . $attrValue;
}
echo $j;
echo '</td>';
echo '</tr>';
//foreach ($parent->children() as $node) {
//foo($node);
foo($parent->children());
//}
}
else {
$j = $parent->getName();
foreach($parent->attributes() as $attrName => $attrValue) {
$j .= ' ' . $attrName. ' : ' . $attrValue;
}
$j .= ' ' . $parent;
echo '<td>';
echo $j;
echo '</td>';
}
echo '</tr>';
}
echo '<body>';
echo '<table border="1">';
foo($test);
echo '</table>';
echo '</body>';
echo '</html>';
すべての要素にヒットし、単純なテーブルをエコーしますが、XML の構造を反映したテーブルを作成したいと考えています。<tr>
と<td
タグのさまざまな設定を試しましたが、うまくいきません。私はいつもネストされた行と列が多すぎて、クレイジーな迷路のように見えます。