Salve a tutti,
ho il seguente oggetto:
Che crea un oggetto simpleXml con una serie di metodi (che ometto tanto non ci servono) per la gestione degli oggetti xml.codice:class xmlDocument { private $sAtRoot; private $sAtVersion; private $sAtEncoding; private $oAtDocument; // ### COSTRUTTORE ### function xmlDocument($fStrDefaultElem, $blnIsUrl=true, $fStrVersion="1.0", $fStrEncoding="UTF-8"){ if (!$blnIsUrl){ $this->sAtRoot=$fStrDefaultElem; $this->sAtVersion=$fStrVersion; $this->sAtEncoding=$fStrEncoding; $strWellFormed = $this->createWellFormedXMLString(); $this->oAtDocument = new SimpleXMLElement($strWellFormed); }else{ if (file_exists($fStrDefaultElem)) { // ### ROOT ### $strDocument= file_get_contents ($fStrDefaultElem); $arrDocument = explode ("\n",$strDocument,2); $strXmlHeader = $arrDocument[0]; $strXmlContent = $arrDocument[1]; // ### ROOT ### $iPosAngolar = strpos($strXmlContent,">"); $iPosSpace = strpos($strXmlContent," "); $iPos = ($iPosSpace < $iPosAngolar) ? $iPosSpace : $iPosAngolar; $strRoot = substr($strXmlContent,1,$iPos-1); // ### VERSION ### $strVersion = preg_replace ("/<(.*?)version=\"(.*?)\"(.*?)>/", "$2", $strXmlHeader); // ### ENCODING ### $strEncoding = preg_replace ("/<(.*?)encoding=\"(.*?)\"(.*?)>/", "$2", $strXmlHeader); $this->sAtRoot=$strRoot; $this->sAtVersion=$strVersion; $this->sAtEncoding=$strEncoding; $this->oAtDocument = new SimpleXMLElement($strDocument); }else{ die("Il file " . $fStrDefaultElem . " non esiste, impossibile caricarlo."); } } }
Partendo da un file, lo carico, lo parso e rimuovo un nodo.
I metodi che uso sono questi:
$o = $oConFile->getElementsFromAttributeValue("track","performanc eId","15");
$oConFile->deleteNode($o[0]);
$oConFile->saveDocumentInFile(complete);
ecco come sono definiti:
Aggiungo anche il toString chiamato nel metodo che stampa i dati nel file.codice:function getElementsFromAttributeValue($fStrNodeName, $fStrAttributeName,$fStrAttributeValue){ $strXPathString = "//" . $fStrNodeName. "[@" . $fStrAttributeName . "='" . $fStrAttributeValue."']"; $arrElements = $this->oAtDocument->xpath($strXPathString); return (count($arrElements)!=0) ? $arrElements: false; // ? false : true; } function deleteNode($fObjElement){ echo $fObjElement; unset($fObjElement); } // ### CONVERTE IN STRINGA IL DOCUMENTO ### function toString(){ $strDocument = $this->oAtDocument->asXML(); // ### GESTISCO SPAZI E A CAPO ### $strDocument = str_replace("><", ">\n<", $strDocument); $strRoot = $this->sAtRoot; $strDocument = preg_replace("/(<" . $strRoot . ".*?>)(\n)?/","$1\n",$strDocument); return $strDocument; } // ### STAMPA IN OUTPUT IL DOCUMENTO CORRENTE ### function saveDocumentInFile($fStrPath){ $resXmlFile = fopen($fStrPath,"w+"); $strDocument = $this->toString(); $strDocument = str_replace("&#39","'",$strDocument); $strDocument = str_replace("&#60","<",$strDocument); $strDocument = str_replace("&#62",">",$strDocument); $strDocument = str_replace("&#36","$",$strDocument); $strDocument = str_replace("&#92","\",$strDocument); fwrite($resXmlFile,$strDocument); fclose($resXmlFile); }
Il problema è che quando faccio l'unset il nodo non viene rimosso. Sia che io stampi la stringa, sia che controlli il contenuto del file. Il nodo esiste, perché se stampo a video il suo contenuto lo visualizzo correttamente. Qualcuno sa darmi qualche dritta?
Scusate per il posto lungo ma volevo essere più chiaro possibile.
Grazie a chi mi aiuterà.
ps: non ditemi USA DOM, ho la necessità di usare simplexml quindi devo trovare la soluzione con questa classe.


Rispondi quotando