Q:

How to solve the issue of multi-level XML using Dom Document in PHP? 

I have an array like ip=["A-B-C-D","A-B-E-D","A-B-F-D"].  Now I have to create an XML based on this hierarchy.  The expected output of the XML will be. 
<start>
<comm>
  <name>A</name>
    <comm>
      <name>B</name>
        <comm>
          <name>C</name>
            <comm>
              <name>D</name>
            </comm>
        </comm>
        <comm>
          <name>E</name>
            <comm>
              <name>D</name>
            </comm>
        </comm>
        <comm>
          <name>F</name>
            <comm>
              <name>D</name>
            </comm>
        </comm>
    </comm>
</comm>

Basically, C, E, and F will be siblings and they'll be inside the sections <comm><name>C/E/F</name></comm> and then inside that tag will be D. Every element needs to be inside the comm and the name tags. 
I have already done the code which looks like this,
$basecomm = $doc->createElement( "start" );
$doc->appendChild( $basecomm );
$base = $doc->getElementsByTagName("comm");

foreach($ip as $input){
    $arr=explode("-",$input);

    foreach ($arr as $a){
        $newcomm=null;
        foreach ( $base as $community )   {

            foreach($community->childNodes as $nextElement){
            if ( $nextElement instanceof DOMElement
                    && $nextElement->tagName == 'name' && $nextElement->nodeValue==$a)   {
                        $newcomm = $nextElement->parentNode;
                    }
        }
        }
        if($newcomm==null){
        $newcomm=$doc->createElement("comm");
        $xml_comm_name=$doc->createElement("name",htmlspecialchars($a));
        $newcomm->appendChild($xml_comm_name);
        $basecomm->appendChild($newcomm);
        }
        $basecomm=$newcomm;
}
}

My problem is that the entire XML is generating properly but the value D is only appearing as a child of C and not for E and F. So how can I fix my code to do that?  Any help is appreciated.  Thanks. 

A:

Your problem is that you're not checking child elements in the context of their parent.  So when you check the D of 'A-B-E-D' it's just checking "is there a D".  It's not asking "is there a D with parent E".  So it thinks the D is already present. 
If you reset the set of valid elements to check to the correct parent each time you start from the top of the tree, you should get the right results.  
<? php
    $ip=["A-B-C-D","A-B-E-D","A-B-F-D","G-H","G-B","X-Y-G-B"];

    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;

    $basecomm = $doc->createElement( "start" );
    $doc->appendChild( $basecomm );

    foreach($ip as $input){
        $base = $doc->getElementsByTagName("comm");
        $currentcomm = $basecomm;

        $arr = explode("-", $input);

        foreach ($arr as $k => $a) {
            $parentComm = null;

            foreach ( $base as $x => $community ) {
                foreach ($community->childNodes as $nextElement) {
                    if ($nextElement->tagName == 'name' && $nextElement->nodeValue==$a) {
                        $parentComm = $nextElement->parentNode;
                    }
                }
            }

            if ($parentComm==null){
                $newcomm=$doc->createElement("comm");
                $xml_comm_name=$doc->createElement("name",htmlspecialchars($a));
                $newcomm->appendChild($xml_comm_name);
                $currentcomm->appendChild($newcomm);
                $base = $newcomm->getElementsByTagName("comm");
                $currentcomm = $newcomm;
            } else {
                $base = $parentComm->getElementsByTagName("comm");
                $currentcomm = $parentComm;
            }
        }
    }

    echo $doc->saveXML();

This is the output, which I think it what you're after.  
<? xml version="1.0"? >
<start>
  <comm>
    <name>A</name>
    <comm>
      <name>B</name>
      <comm>
        <name>C</name>
        <comm>
          <name>D</name>
        </comm>
      </comm>
      <comm>
        <name>E</name>
        <comm>
          <name>D</name>
        </comm>
      </comm>
      <comm>
        <name>F</name>
        <comm>
          <name>D</name>
        </comm>
      </comm>
    </comm>
  </comm>
  <comm>
    <name>G</name>
    <comm>
      <name>H</name>
    </comm>
    <comm>
      <name>B</name>
    </comm>
  </comm>
  <comm>
    <name>X</name>
    <comm>
      <name>Y</name>
      <comm>
        <name>G</name>
        <comm>
          <name>B</name>
        </comm>
      </comm>
    </comm>
  </comm>
</start>

