*/ public $_charset; public $_lang; /** * xml elements definition for the main element */ protected $_main = array ( 'mandatory' => array (), 'optionnal' => array ()); /** * simplexml base node */ protected $_xml_base_node_name; protected $_xml_base_node_name_attributes = array (); protected $_xml_main_node_name; /** * simplexml data element */ public $_xml_dom; protected $_xml_main_node; protected $_xml_xpath; /** * array of elements */ public $_item_list = array (); /** * Class constructor * * @param array * @return void */ function __construct ($attributes = array()) { global $gJConfig; $this->_charset = $gJConfig->defaultCharset; $this->_lang = $gJConfig->defaultLocale; // Definition of the main XML structure $this->_xml_dom = new DOMDocument('1.0', $this->_charset);; $root_node = $this->_xml_dom->createElement ( $this->_xml_base_node_name); foreach ($this->_xml_base_node_name_attributes as $attribute => $value) { $root_node->setAttribute ( $attribute, $value); } $this->_xml_dom->appendChild ($root_node); $this->_xml_main_node = $this->_xml_dom->createElement ( $this->_xml_main_node_name); $this->_createBaseStructure (); $root_node->appendChild ($this->_xml_main_node); $this->_xml_xpath = new DOMXPath ($this->_xml_dom); parent::__construct ($attributes); } /** * Generating the base structure * * @param array $baseArray * @param DOMNode $parentNode * @return void */ private function _createBaseStructure ( $baseArray = null, $parentNode = null) { if (is_null ($baseArray)) { $baseArray = $this->_main; } if (is_null ($parentNode)) { $parentNode = $this->_xml_main_node; } foreach (array ('mandatory', 'optionnal') as $nodeType) { if (isset ($baseArray[$nodeType])) { foreach ($baseArray[$nodeType] as $nodeName => $nodeValue) { if ($nodeName[0] == '_') { // We have an attribute $parentNode->setAttribute ( substr($nodeName, 1), $nodeValue); } else { $node = $this->_xml_dom->createElement ($nodeName); if (is_array ($nodeValue)) { $this->_createBaseStructure ($nodeValue, $node); } $parentNode->appendChild ($node); } } } } } /** * Setting an xpath node * * @param string xpathString * @param string nodeValue * @return void */ public function set ($xpathString, $nodeValue) { $nodeList = $this->_xml_xpath->query ( '//' . $this->_xml_base_node_name . '/' . $this->_xml_main_node_name . '/' .$xpathString); if ($nodeList->length != 0) { if (preg_match ("/^[\d\w\s_]+$/", $nodeValue)) { $nodeList->item(0)->nodeValue = $nodeValue; } else { $nodeList->item(0)->appendChild ( $this->_xml_dom->createCDATASection( $nodeValue)); } return true; } return false; } /** * Setting an xpath node * * @param string xpathString * @param string nodeValue * @return void */ public function _deleteOptionnalEmptyNodes () { foreach ($this->_main['optionnal'] as $nodeName => $nodeValue) { $nodeList = $this->_xml_xpath->query ( '//' . $this->_xml_base_node_name . '/' . $this->_xml_main_node_name . '/' .$nodeName); if ($nodeList->length != 0) { if ($nodeList->item(0)->nodeValue == '') { $this->_xml_main_node->removeChild ( $nodeList->item(0)); } } } } } abstract class jXMLFeedItem { protected $_xml_item_node; /** * xml item definition for any item element */ protected $_item = array ( 'mandatory' => array (), 'optionnal' => array ()); function __construct ( $DOMDocument, $parentNode, $dataArray) { $item_node = $DOMDocument->createElement ( $this->_xml_item_node); $parentNode->appendChild ( $item_node); $this->_createBaseStructure ( $DOMDocument, $this->_item, $item_node, $dataArray); } /** * Generating the base structure * * @return void */ private function _createBaseStructure ( $DOMDocument, $baseArray, $parentNode, $dataArray) { foreach (array ('mandatory', 'optionnal') as $nodeType) { if (isset ($baseArray[$nodeType])) { foreach ($baseArray[$nodeType] as $nodeName => $nodeValue) { if ($nodeName[0] == '_') { // We have an attribute $parentNode->setAttribute ( substr($nodeName, 1), (isset($dataArray[$nodeName]) ? $dataArray[$nodeName] : $nodeValue)); } else { $node = $DOMDocument->createElement ($nodeName); if (is_array ($nodeValue)) { $this->_createBaseStructure ( $DOMDocument, $nodeValue, $node, (isset($dataArray[$nodeName]) ? $dataArray[$nodeName] : $nodeValue)); } if (isset ($dataArray[$nodeName])) { if (preg_match ("/^[\d\w\s_]+$/", $dataArray[$nodeName])) { $node->nodeValue = $dataArray[$nodeName]; } else { $node->appendChild ( $DOMDocument->createCDATASection( $dataArray[$nodeName])); } } $parentNode->appendChild ($node); } } } } } } ?>