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


i have array ip=["a-b-c-d","a-b-e-d","a-b-f-d"]. have create xml based on hierarchy. expected output of xml 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, , f siblings , they'll inside sections <comm><name>c/e/f</name></comm> , inside tag d. every element needs inside comm , name tags.

i have done code looks this,

$basecomm = $doc->createelement( "start" ); $doc->appendchild( $basecomm ); $base = $doc->getelementsbytagname("comm");  foreach($ip $input){     $arr=explode("-",$input);       foreach ($arr $a){         $newcomm=null;         foreach ( $base $community )   {              foreach($community->childnodes $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 entire xml generating value d appearing child of c , not e , f. how can fix code that? appreciated. thanks.

your problem you're not checking child elements in context of parent. when check d of 'a-b-e-d' it's checking "is there d". it's not asking "is there d parent e". thinks d present.

if reset set of valid elements check correct parent each time start top of tree, should 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 $input){         $base = $doc->getelementsbytagname("comm");         $currentcomm = $basecomm;          $arr = explode("-", $input);          foreach ($arr $k => $a) {             $parentcomm = null;              foreach ( $base $x => $community ) {                 foreach ($community->childnodes $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 output, think 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> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -