1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?php
/*include_once("utilities.php");*/
/*
* Class adding <MYNODE> </MYNODE> in the XML flow in adding the id of the user.
*/
class AddMYNODE2XML {
private $strXmlOriginal = '';
private $strXmlModified = '';
private $MYNODE = 0;
private $xml;
private $error;
public function __construct($strXmlFlow, $MYNODE) {
if (isset ($strXmlFlow)){
$this->strXmlOriginal = $strXmlFlow;
$this->xml = new SimpleXMLElement($strXmlFlow);
$this->MYNODE = $MYNODE;
$this->strXmlModified = $this->handle_MYNODE($this->xml);
}
else {//function to display an error message
$this->error = 'Error parameter constructeur AddMYNODE2XMLFlow::__construct file:class_addMYNODE.php';
}
}
public function __destruct(){}
/*************** function that modify the simpleXMLElement given as parameter
* @param simpleXMLElement xml_SimpleXML The SimpleXMLElement to transform
* @return string
*******************/
function handle_MYNODE($xml_SimpleXML) {
if ($xml_SimpleXML === false) {
$this->error = 'Error reading XML string in AddMYNODE2XMLFlow::handleFlow_MYNODE file:class_addMYNODE.php';
}
else {
// get the name of the first child of the root element
$tab = array();
foreach ($xml_SimpleXML->children() as $second_gen) {
$tab[] = $second_gen;
}
$firstNodeName = $tab[0]->getName();
// transformation of the simpleXML object in a DomDocument
$dom_sxe = dom_import_simplexml($xml_SimpleXML);
if ($dom_sxe) {
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
// creation of the node to add in the XML structure
$MYNODENode = $dom->createElement('MYNODE', $this->MYNODE);
// get the list of node which have the same name as the first child of the root element
$firstNodes = $dom->getElementsByTagName($firstNodeName);
$firstNode = $firstNodes->item(0) ;
// insertion of the new element as the top child of the root element
$dom_sxe->insertBefore($MYNODENode, $firstNode);
$SXE = simplexml_import_dom($dom_sxe);
if (isset ($SXE)) {
$this->xml = $SXE;
return $SXE->asXML();
}
else {
$this->error = 'Error during XML conversion DOM->SimpleXML in AddMYNODE2XMLFlow::handleFlow_MYNODE file:class_addMYNODE.php';
}
}
else {
$this->error = 'Error during XML conversion SimpleXML->DOM in AddMYNODE2XMLFlow::handleFlow_MYNODE file:class_addMYNODE.php';
}
}
}
function getStrXmlModified() {
return encode_to_html($this->strXmlModified);
}
function getStrXmlOriginal() {
return encode_to_html($this->strXmlOriginal);
}
function getXml() {
return $this->xml;
}
}//end class
?> |
Partager