我试图通过simpleDOM和PHP分割XML文件。有几个节点,我需要精确复制每个子节点,然后将它们导出到一个单独的XML文件中。我已经能够提取所有子项和属性并将它们回显到PHP中,但我无法弄清楚如何正确地将它们作为XML。我尝试使用cloneNode函数,但它只是返回一个很大的空白空间,表明它里面有子节点。我能够让它拉出顶级值,但不能拉出子节点或属性。我能在这做什么?
<?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);
?>
这是我的输出
<?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>