Nicholas Mansfield
Nicholas Mansfield

Reputation: 140

Trying to copy a node in simpleXML , but it isn't copying attributes or children

I'm trying to split up an XML file via simpleDOM and PHP. There are several nodes, and I need to exactly copy each one and all of its children, then export them to a separate XML file. I have been able to extract all the children and the attributes and echo them into PHP, but I can't figure out how to properly make them as XML. I tried using the cloneNode function, but it just returned a big empty space, indicating that it had children inside. I'm able to have it pull the top level values, but not the child nodes or the attributes. What can I do here?

<?php

include 'SimpleDOM.php';


$testXML = simpledom_load_file("testBase.xml") 
    or die("Can't find file, brah");
$outputXML = new DOMDocument("1.0"); //This will be reset after each file export.  

function generateXML(){
//sets up the xml for out put


$xmlRoot = $outputXML ->createElement("Item");  
}
//generateXML();


$xmlRoot = $outputXML ->createElement("Itemz"); 
$outputXML->appendChild($xmlRoot);




$outputXML->formatOutput=true;
//echo "<xmp>".$outputXML->saveXML()."</xmp>";
$outputXML->save("xmlOutTest.xml") or die("ERROR SAVING XML");






function processXML($node){

$saveName="defaultSaveName";
$outputXML = new DOMDocument("1.0");
$xmlRoot = $outputXML ->createElement("Item");


  foreach($node->children() as $item => $data){
      if($item =="Item"){
            echo "<h2>NEW ITEM </h2> <br/>";
            $saveName="defaultSaveName";
            $outputXML = new DOMDocument("1.0");
            //$xmlRoot = $outputXML ->createElement("Item");
            //$outputXML->appendChild($xmlRoot);
            //$newElement =     $outputXML->createElement("Itemz",$data->cloneNode(true));
            //$outputXML->appendChild($newElement);
            //$outputXML->appendChild($data->cloneNode(true));  

            foreach($data as $itemName=>$itemInfo){
            echo $itemInfo."<br />";    
            $newElement =     $outputXML->createElement($itemName,$itemInfo);
            $outputXML->appendChild($newElement);
            }


      }

     // $newElement = $outputXML->createElement($item,$data);
        //  $outputXML->appendChild($newElement);


      echo $item.": ".$data;
      foreach($data->attributes() as $attribName=>$attribData){
          if($attribName=="code"){
            $saveName= $attribData;  
          }
        echo $attribName    .'="'.$attribData."<br/>";
      }


   echo "<br />";

    if($item =="Item"){
          $outputXML->formatOutput=true;
        echo "<xmp>".$outputXML->saveXML()."</xmp>";
        $outputXML->save($saveName.".xml") or die("ERROR SAVING XML");
      }
   processXML($data);
  }
}

processXML($testXML);


?>

Here is my output

<?xml version="1.0"?>
<Description>ITEM DESCRIPTION</Description>
<ExtraDescription>99999999</ExtraDescription>
<IsSalesItem>1</IsSalesItem>
<IsStockItem>0</IsStockItem>
<IsPurchaseItem>1</IsPurchaseItem>
<IsFractionAllowedItem>0</IsFractionAllowedItem>
<IsMakeItem>0</IsMakeItem>
<IsSubcontractedItem>0</IsSubcontractedItem>
<IsOnDemandItem>0</IsOnDemandItem>
<IsWebshopItem>0</IsWebshopItem>
<CopyRemarks>0</CopyRemarks>
<IsSerialItem>0</IsSerialItem>
<IsBatchItem>0</IsBatchItem>
<Assortment>






        </Assortment>
<Sales>


        </Sales>
<Costs>

        </Costs>
<DateStart>2014-06-01</DateStart>
<Note>6</Note>
<ItemPrice>







        </ItemPrice>
<ItemCategory>

        </ItemCategory>
<ItemAccounts>

        </ItemAccounts>
<ItemWarehouses>

        </ItemWarehouses>

Upvotes: 0

Views: 359

Answers (0)

Related Questions