PHP - 将动态XML对象转换为HTML列表

时间:2017-11-07 12:30:10

标签: php html xml

我正在使用PHP,我编写了一个应用程序,用数据库查询的结果创建并填充SimpleXMLObject。结果将保存为标记。

有关应用程序的背景信息:

我从该查询得到的变量就像这个“a / b / c”,我使用explode来拆分这个变量,并将每个单独的实例添加为一个自己的XML标签。

例如$parts = explode("/", $cropSpace);

这意味着我事先不知道有多少变量是使用explode创建的,以及这些节点的名称是什么。

我想要完成的任务:

我想将动态生成的节点和大小的XML对象转换为HTML列表(<ul><li>

我尝试了什么:

我查找了许多关于如何完成这样的事情的指南,但是所有这些指南都集中在一个静态XML对象/文件中,你已经知道了节点的名称。由于我不知道节点的名称和对象的大小,我自己尝试了这个

foreach ($parts as $part) {
      if (!is_numeric($part)) {
         if (!isset($set->$part)) {
            $set = $set->addChild($part);
            echo "<ul><li>$part</li>";
            $counter++;
         } else {
            $set = $set->$part;
                }
      for ($i = 0; $i < $counter - 1; $i++){
      echo "</ul>";
      }
}

我知道为什么它不起作用(与创建XML对象不同,它不检查该列表是否已经创建)但我不知道该怎么做。

预期输出示例 例如,我有这四个变量

  • A / B / C / d
  • a / b / c / e
  • A / B / F
  • 克/小时 然后创建XML对象,如this /包含this.

我希望这个XML对象显示在像this.

这样的HTML列表中

我知道要阅读很多,但我真的很乐意帮助。提前谢谢。

修改

以下是原始XML文件的一个示例,但它非常大并且会为每个用户进行更改。

<?xml version="1.0"?>
<permissions>
    <sogo>
        <jost>
            <Calendar>
                <PersönlicherKalender>
                    <ConfidentialModifier/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <PrivateModifier/>
                    <PublicModifier/>
                </PersönlicherKalender>
                <IfIKalenderBJ>
                    <ConfidentialModifier/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <PrivateModifier/>
                    <PublicModifier/>
                </IfIKalenderBJ>
            </Calendar>
        </jost>
        <ificalendars>
            <Calendar>
                <Meetings>
                    <ObjectEraser/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <ObjectCreator/>
                    <PublicModifier/>
                    <ConfidentialDAndTViewer/>
                    <PublicViewer/>
                    <PublicViewer/>
                </Meetings>
                <Events>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <PublicModifier/>
                    <ConfidentialDAndTViewer/>
                    <PublicViewer/>
                    <PublicViewer/>
                </Events>
            </Calendar>
        </ificalendars>
        <ifiabsences>
            <Calendar>
                <Barbara>
                    <PublicModifier/>
                    <ConfidentialModifier/>
                    <PrivateModifier/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <PublicViewer/>
                    <PublicViewer/>
                </Barbara>
                <Nico......meetings>
                    <PublicViewer/>
                </Nico......meetings>
                <PublicViewer/>
                <Erica......meetings>
                    <PublicViewer/>
                </Erica......meetings>
                <PublicViewer/>
                <Tammy......meetings>
                    <PublicViewer/>
                </Tammy......meetings>
                <PublicViewer/>
                <Daniela......meetings>
                    <PublicViewer/>
                </Daniela......meetings>
                <PublicViewer/>
                <Marina......meetings>
                    <PublicViewer/>
                </Marina......meetings>
                <PublicViewer/>
                <Mona......meetings>
                    <PublicViewer/>
                </Mona......meetings>
                <PublicViewer/>
                <Enrico......meetings>
                    <PublicViewer/>
                </Enrico......meetings>
                <PublicViewer/>
                <Claudia......meetings>
                    <PublicViewer/>
                </Claudia......meetings>
                <PublicViewer/>
                <Beat......meetings>
                    <PublicViewer/>
                </Beat......meetings>
                <PublicViewer/>
                <Nathan......meetings>
                    <PublicViewer/>
                </Nathan......meetings>
                <PublicViewer/>
                <Hanspeter......meetings>
                    <PublicViewer/>
                </Hanspeter......meetings>
                <PublicViewer/>
                <Elida......meetings>
                    <PublicViewer/>
                </Elida......meetings>
                <PublicViewer/>
                <Kim......meetings>
                    <PublicViewer/>
                </Kim......meetings>
                <PublicViewer/>
            </Calendar>
        </ifiabsences>
        <ifilehrbereich......lehrbereich>
            <Calendar......lehrbereich>
                <PlanungLehre......lehrbereich>
                    <PublicModifier/>
                </PlanungLehre......lehrbereich>
            </Calendar......lehrbereich>
        </ifilehrbereich......lehrbereich>
        <Calendar......lehrbereich>
            <PlanungLehre......lehrbereich>
                <PublicModifier/>
            </PlanungLehre......lehrbereich>
        </Calendar......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <PlanungLehre......lehrbereich>
            <PublicModifier/>
        </PlanungLehre......lehrbereich>
        <Lehrbereich......lehrbereich>
            <PublicModifier/>
        </Lehrbereich......lehrbereich>
        <PublicModifier/>
        <ifistundenplan......stundenplan>
            <Calendar......stundenplan>
                <Ass......stundenplan>
                    <PublicModifier/>
                </Ass......stundenplan>
            </Calendar......stundenplan>
        </ifistundenplan......stundenplan>
        <Calendar......stundenplan>
            <Ass......stundenplan>
                <PublicModifier/>
            </Ass......stundenplan>
        </Calendar......stundenplan>
        <Ass......stundenplan>
            <PublicModifier/>
        </Ass......stundenplan>
        <PublicModifier/>
        <PublicModifier/>
        <CL......stundenplan>
            <PublicModifier/>
        </CL......stundenplan>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BSc......stundenplan>
            <PublicModifier/>
        </BSc......stundenplan>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <MSc......stundenplan>
            <PublicModifier/>
        </MSc......stundenplan>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <ifireservations......raumreservation>
            <Calendar......raumreservation>
                <BIN0.B.04......raumreservation>
                    <PublicModifier/>
                </BIN0.B.04......raumreservation>
            </Calendar......raumreservation>
        </ifireservations......raumreservation>
        <Calendar......raumreservation>
            <BIN0.B.04......raumreservation>
                <PublicModifier/>
            </BIN0.B.04......raumreservation>
        </Calendar......raumreservation>
        <BIN0.B.04......raumreservation>
            <PublicModifier/>
        </BIN0.B.04......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <BIN0.B.06......raumreservation>
            <PublicModifier/>
        </BIN0.B.06......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BIN1.D.29......raumreservation>
            <PublicModifier/>
        </BIN1.D.29......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BIN2.A.01......raumreservation>
            <PublicModifier/>
        </BIN2.A.01......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BIN2.A.06......raumreservation>
            <PublicModifier/>
        </BIN2.A.06......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BIN2.D.27......raumreservation>
            <PublicModifier/>
        </BIN2.D.27......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <BIN2.E.23......raumreservation>
            <PublicModifier/>
        </BIN2.E.23......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <RAID-017......raumreservation>
            <PublicModifier/>
        </RAID-017......raumreservation>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <PublicModifier/>
        <ifireservations......meetings>
            <Calendar......meetings>
                <Coffeemachine1......meetings>
                    <PublicViewer/>
                </Coffeemachine1......meetings>
            </Calendar......meetings>
        </ifireservations......meetings>
        <Calendar......meetings>
            <Coffeemachine1......meetings>
                <PublicViewer/>
            </Coffeemachine1......meetings>
        </Calendar......meetings>
        <Coffeemachine1......meetings>
            <PublicViewer/>
        </Coffeemachine1......meetings>
        <PublicViewer/>
        <PublicViewer/>
        <Coffeemachine2......meetings>
            <PublicViewer/>
        </Coffeemachine2......meetings>
        <PublicViewer/>
        <PublicViewer/>
        <PublicViewer/>
        <PublicViewer/>
        <Coffeemachine3......meetings>
            <PublicViewer/>
        </Coffeemachine3......meetings>
        <PublicViewer/>
        <PublicViewer/>
        <PublicViewer/>
        <PublicViewer/>
    </sogo>
</permissions>

3 个答案:

答案 0 :(得分:1)

首先,您需要阅读文件:

<?php
    $xml=simplexml_load_file("yourfilename.xml");
?>

然后把它变成这样的列表:

<ul id="interface">
    <?php foreach($xml->sogo->jost->Calendar->PersönlicherKalender as $Kalender){ ?>
    <li>
    <?php echo $Kalender; ?>
    </li>
    <?php } ?>
</ul>

这将为每个PersönlicherKalender生成列表。对其他数据也这样做。

答案 1 :(得分:0)

你基本上有两种方法:

XSL(T)或DIY

<强> XSL(T)

XSL是一种描述XML转换的语言(“样式表”)。由于您的输出是HTML,这应该很好。

XSLT确实有学习曲线,并不是那么受欢迎。但它被设计为一种将XML设计为输出的方式,正是您正在尝试做的事情。

更多信息:

[完全披露:我从未在PHP中使用过XSLT]

<强> DIY

基本上如果转换足够简单,你可以相对容易地让PHP将XML解析为内存中的数据结构,而不必自己解析(注意巨大的XML文件)

一旦有了内存数据结构,就可以根据需要使用简单的循环和/或测试来枚举它,以提取所需的数据或使用面向对象的迭代器并生成所需的输出。

在PHP中阅读XML:有几种方法可以做到这一点,但是simplexml方法可以让你以最少的努力学习它所需要的东西。

一些可以帮助您入门的指示:

答案 2 :(得分:0)

我不完全确定我完全理解你的目标。也许这适合你或至少接近。

有几个在http://php.net/manual/en/simplexmlelement.children.php

处递归XML的示例
<?php

class XmlRecurser {

    public $output;
    const Indent = 2;

    function recurse($x) {
        $this->output = "<ul>\n";
        $this->output .= $this->space(self::Indent) . "<li>" . $x->getName() . "</li>\n";

        $this->recurseXML($x, self::Indent);

        $this->output .= "</ul>\n";
    }

    function recurseXML($x, $indent = 0, $parent="") { 
        $child_count = 0; 
        foreach($x as $key => $child) { 
            $child_count++;     
            $this->output .=  $this->space($indent) . "<ul>\n" .
                $this->space($indent+self::Indent) . "<li>" . $key . "</li>\n";
                // Walk through attributes on this node.
                foreach ($child->attributes() as $attrkey => $attrvalue) {
                    $this->output .= 
                        $this->space($indent+self::Indent) . "attribute: " . $attrkey  . '=' . $attrvalue .  "\n";
                }


            // $this->output .=  str_repeat(' ', $indent) . '<' . $key . ">\n";
            // no children, aka "leaf node" 
            if ( $this->recurseXML($child, $indent+self::Indent, $parent.".".$key) == 0 )  { 
                //print($parent . "." . (string)$key . " = " . (string)$value . "<BR>\n");        

            }
            $this->output .=  $this->space($indent) . "</ul>\n";

            // $this->output .=  str_repeat(' ', $indent) . '</' .  $key . ">\n";
        } 
        return $child_count; 
    }

    function space($n) {
        return str_repeat(' ', $n);
    }

} 

// Some sample XML. 
$xml = '
<permissions>
    <sogo sogo-attr1="sogo-value-1" sogo-attr2="sogo-value-2">
        <jost jost-attr1="jost-value-1" jost-attr2="jost-value-2">
            <Calendar>
                <PersönlicherKalender>
                    <ConfidentialModifier/>
                    <ObjectCreator/>
                    <ObjectEraser/>
                    <PrivateModifier/>
                    <PublicModifier/>
                </PersönlicherKalender>
                </Calendar>
        </jost>
    </sogo>
</permissions>
';

$x = simplexml_load_string($xml);
$r = new XmlRecurser;
$s = $r->recurse($x);
echo $r->output;