Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP从XML创建html表_Php_Html_Xml_Algorithm - Fatal编程技术网

使用PHP从XML创建html表

使用PHP从XML创建html表,php,html,xml,algorithm,Php,Html,Xml,Algorithm,我正在尝试使用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> <

我正在尝试使用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>

50
50
以及我目前正在使用的功能:

    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>';
函数foo($parent){ 如果($parent->children()->count()>0){ $j='.$parent->getName(); foreach($parent->attributes()作为$attrName=>$attrValue){ $j.=''.$attrName.='.$attrValue; } echo$j; 回声'; 回声'; //foreach($parent->children()作为$node){ //foo($节点); foo($parent->children()); //} } 否则{ $j=$parent->getName(); foreach($parent->attributes()作为$attrName=>$attrValue){ $j.=''.$attrName'.:'.$attrValue; } $j.=''.$parent; 回声'; echo$j; 回声'; } 回声'; } 回声'; 回声'; foo($测试); 回声'; 回声'; 回声';
它命中所有元素并响应一个简单的表,但我想生成一个反映XML结构的表。我已经为
尝试了许多不同的设置,
您的xml是否规范化,以便表中的实际列数是静态的?如果是这样的话,那么每个外部元素在表中可以有几行(其中外部元素在每一行上为其子元素重复)?还是像每个外部元素=一行那样简单?如果是后者,为什么不在PHP之外构建表并动态插入行呢?另外,您是否使用simplexml或DOM作为xml抽象?simplexml,但我不确定在PHP之外构建它是什么意思。使用XLST?不,不是XSLT,尽管这是一种可行的选择。我的意思是拥有一个表行数组,然后在输出数组中每一行的simplexml解析位之外使用foreach。这样,您可以首先使用
print\r
输出数组,查看表语法是否有问题,或者循环本身是否有问题。如果您可以显示希望最终得到的html表(请将其格式化为html代码,而不是实际呈现的html),这也会有所帮助。