0

I'm using PHP and I wrote an application that creates and fills a SimpleXMLObject with the results of a database query. The results are saved as tags.

Background information on the application:

The variables I get from that query are like this "a/b/c" and I use explode to split this variable and add each individual instance as an own XML-tag.

For example $parts = explode("/", $cropSpace);

So that means I never know beforehand how many variables are created with explode and what the names of those nodes are.

What I want to accomplish:

I want to transform this XML object with dynamically generated nodes and size into an HTML list (<ul><li>)

What I tried:

I looked up many guides on how to accomplish such a thing but all of them are focused on a static XML object/file in which you already know the names of the nodes. Since I dont know the names of the nodes and the size of the object, I tried on my own this

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>";
      }
}

I know why it isn't working (unlike in the creating the XML object it doesn't check if that list was already created) but i don't know how to do it.

Example Expected Output: For example I have these four variables

  • a/b/c/d
  • a/b/c/e
  • a/b/f
  • g/h Then the XML object is created like this / contains this.

And I want this XML object to be displayed in a HTML list like this.

I know it is a lot to read but I would really appriciate the help. Thanks in advance.

EDIT

Here is an example of the original XML File but it is very big and changes for every user.

<?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>
2
  • Can you give a sample of the xml file? Commented Nov 7, 2017 at 12:33
  • You mean the original XML File i use in my application? If yes sure but i posted a link to a picture of an example XML File Commented Nov 7, 2017 at 12:34

3 Answers 3

1

First, you need to read your file:

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

Then turn it into list like this:

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

This will generate list for every PersönlicherKalender. Do the same for other data.

Sign up to request clarification or add additional context in comments.

1 Comment

For it to work on your suggested solution, I would need to know the name of the nodes, which I don't since it changes for every user. That is exactly what i saw at the guides, they assume that we know every node in the XML object but in my case i don't. And I don't need to read the file since I create with each session a temporary XML object. But thanks for taking the time for the answer.
0

I'm not entirely sure I understand exactly what you are after. Perhaps this does it for you or is at least close.

There are several examples of recursing XML at http://php.net/manual/en/simplexmlelement.children.php

<?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;

5 Comments

Thank you for your answer and your time, I'm going to take a look at it
Yes! This is exactly what I was trying to do. Thank you very much ryantxr :)
Might I ask the reason for the down vote? OP says this is EXACTLY what he wanted so I'm calling BS.
I don't get it either.. Your answer was usable right away (ofc with changing variable names to suit my application) and with the link you sent, I learned how to do it for other cases. I would upvote you but i need 15 reputation points.
You aren't supposed to add more questions to an existing question. You should start a new question once your original is answered. I'm not sure why you could not do this. The answer was on the page link you provided. I don't really understand exactly what you mean by "displays in the list the value of the attributes". I added the attributes to the output and I will leave the exact formatting up to you.
0

You have essentially two ways to go:

XSL(T) or DIY

XSL(T)

XSL is a sort of language ("stylesheet") that describes a transformation of XML. Since your output is HTML, this should work pretty well.

XSLT does have a learning curve and isn't all that popular. But it is designed as a way to style XML into output, exactly what you're trying to do.

More info:

[Full disclosure: I've never used XSLT in PHP]

DIY

Essentially if the transformation is simple enough, you can relatively easy let PHP parse the XML into a data structure in memory without having to parse it yourself (take care with gigantic XML files)

Once you have the in memory data structure you can enumerate through it as needed using simple loops and/or tests to extract the data you need or use object oriented iterators and produce the required output.

Reading XML in PHP: there's a few ways to do it, but the simplexml way should get you what you need with the least effort in learning it all.

Some pointers to get you started:

1 Comment

Thank you for your answer, i looked into XSLT and in the examples it needs to know what the name of the nodes are from which you take the informations so i'm going to try it with /*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.