title: XML File - Get specific child nodes in unlimited node depths
body: <p>I'm working on a website that uses hierarchical data. After struggling to do it with MySQL databases (really complicated...), I decided to dive into XML because it sounds like XML works perfectly for my needs.</p>
  
  <p>Now I'm experimenting with an XML File and SimpleXML. But first of all, here is what my XML File looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  
  &lt;content&gt;
      &lt;parent&gt;
          &lt;child id="1"&gt;
              &lt;title&gt;child 1&lt;/title&gt;
  
              &lt;child id="1"&gt;
                  &lt;title&gt;child 1.1&lt;/title&gt;
  
                  &lt;child id="1"&gt;
                      &lt;title&gt;child 1.1.1&lt;/title&gt;
                  &lt;/child&gt;
              &lt;/child&gt;
  
              &lt;child id="2"&gt;
                  &lt;title&gt;child 1.2&lt;/title&gt;
  
                  &lt;child id="1"&gt;
                      &lt;title&gt;child 1.2.1&lt;/title&gt;
  
                          &lt;child id="1"&gt;
                              &lt;title&gt;child 1.2.1.1&lt;/title&gt;
                          &lt;/child&gt;
                  &lt;/child&gt;
  
                  &lt;child id="2"&gt;
                      &lt;title&gt;child 1.2.2&lt;/title&gt;
                  &lt;/child&gt;
              &lt;/child&gt;
  
              &lt;child id="3"&gt;
                  &lt;title&gt;child 1.3&lt;/title&gt;
              &lt;/child&gt;
          &lt;/child&gt;
      &lt;/parent&gt;
  &lt;/content&gt;
  </code></pre>
  
  <p>As you can see, it has a variing "depth" of child nodes. I also don't know the depth of childs, as they are created by the web app. This depth or  "number of layers" can get quite high.</p>
  
  <p>Now I want to read this XML File in my website. For example, I want to visualize it as a tree, with all the child nodes represented as circles connected to their parent circle.</p>
  
  <p>I've managed to have a foreach getting all the first-layer "child" elements an then another foreach in it getting all second-layer "child" elements. The problem is, that this limits the number of layers I can visualize because I cannot have a dozen nested foreach'es.</p>
  
  <p>Now I already have a headache thinking of a way of "unlimited nested foreach structures" to get all the layers of "child" nodes. But I'm not able to find a way of doing it.</p>
  
  <p>Do you have an idea how to do it? Please help me! Thanks in advance.</p>
  
  <p>PS: Sorry for my english, I'm an german teenager student :)</p>
  
  <p>EDIT: Here is the code in my test.php:</p>
  
  <pre><code>&lt;?php
      if (file_exists('mydata.xml'))
      {
          $xml = simplexml_load_file('mydata.xml');
  ?&gt;
  
  &lt;ul&gt;
  &lt;?php 
          foreach($xml-&gt;parent-&gt;child as $item) // Go through first layer
          {
              echo "&lt;li&gt;".$item-&gt;title;
  
              echo "&lt;ul&gt;"; // Open second layer &lt;ul&gt;
              foreach($item-&gt;child as $item) // Go through second layer
              {
                  echo "&lt;li&gt;".$item-&gt;title."&lt;/li&gt;";
              }
              echo "&lt;/ul&gt;"; // Close second layer &lt;ul&gt;
  
              echo "&lt;/li&gt;"; // Close child &lt;li&gt;
          }
      }
      else
      {
         exit('Konnte Datei nicht laden.');
      }
  ?&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>This is the result, just what I was expecting:</p>
  
  <pre><code>- child 1
  
      - child 1.1
      - child 1.2
      - child 1.3
  </code></pre>
  
  <p>So this works fine, but as mentioned in the comments, I need this not only for layer 1 to 2, but for layer 1 to n. Would really appreciate if someone has an idea :)</p>