/** * Get and/or set attribute value * * @param str new attribute value [optional] * @return str attribute value * @access public */ public function value($sValue = NULL) { if ($sValue) { $this->_sValue = EasyXML::xmlEntities($sValue); } return $this->_sValue; }
// Loop the books foreach ($aBooks as $aBook) { // Add a book node for each book in the array $oBook = $oShelf->createElement('book'); // Loop the contents of the book foreach ($aBook as $sNode => $sValue) { // Create new node of the content type $oNode = $oBook->createElement($sNode); // Add a text node with the content value $oNode->createText($sValue); } // foreach(book) } // foreach(books) // Print the XML document EasyXML::printXML($oEasyXML); ?>
/** * Create an EasyXML document object * * @param obj dom document * @param obj EasyXML document * @return bool * @access private */ private static function _createDocument($oDOM, $oEasyXML, $oXP = NULL, $iNS = 1) { if (!($aChilds = $oDOM->childNodes)) { throw new EasyXML_Exception('Invalid XML document: no root node has been defined'); } foreach ($aChilds as $oDomNode) { switch ($oDomNode->nodeType) { case XML_ELEMENT_NODE: // Create element node $oEasyNode = $oEasyXML->createElement($oDomNode->tagName); // Check for a namespace declaration in the element if (!$oXP) { $oXP = new DOMXPath($oDOM); } if ($oList = $oXP->query('namespace::*', $oDomNode)) { $i = 0; foreach ($oList as $oNS) { $i++; if ($i >= $iNS) { #echo 'NS: '.$item->nodeValue.'. Prefix: '.$item->prefix.' Node: '.$oDomNode->localName.' Node-NS: '.$oDomNode->namespaceURI.chr(10).chr(10); $oEasyNode->createNamespace($oNS->nodeValue, $oNS->prefix); $iNS++; } } } // Add element attributes foreach ($oDomNode->attributes as $n => $v) { $oEasyNode->createAttribute($n, $v->nodeValue); } // Create child nodes of the element EasyXML::_createDocument($oDomNode, $oEasyNode, $oXP, $iNS); break; case XML_ATTRIBUTE_NODE: // Attributes should be added when an element is added and this exception should therefor never be triggered. throw new EasyXML_Exception('Can not add an attribute node while outside an element'); break; case XML_TEXT_NODE: // Create text node $oEasyXML->createText($oDomNode->nodeValue); break; case XML_CDATA_SECTION_NODE: // Create CDATA node $oEasyXML->createCDATA($oDomNode->nodeValue); break; case XML_COMMENT_NODE: // Create comment node $oEasyXML->createComment($oDomNode->nodeValue); break; default: // Throw exception if an unsupported node is hit throw new EasyXML_Exception('Unsupported node type in XML document'); } } return true; }
/** * Set PI Target * * @param str PI Target * @return bool * @access private */ private function _setPITarget($sPITarget) { // Check target name if (preg_match('/^xml$/i', $sPITarget)) { throw new EasyXML_Exception('Processing instruction target cannot be xml, this is a reserved name'); return false; } if (!EasyXML::validNodeName($sPITarget)) { throw new EasyXML_Exception('Invalid Processing instruction target "' . $mName . '"'); } $this->_sPITarget = $sPITarget; return true; }
/** * Create a child processing instruction node * * @param str processing instruction target * @param str processing instruction value/content * @return new child node * @access public */ public function createPI($sPITarget, $sValue = '') { // Check target name if (!EasyXML::validNodeName($sPITarget)) { throw new EasyXML_Exception('Invalid Processing instruction target "' . $sPITarget . '"'); } $oChild = new EasyXML_ProcessingInstruction($this, $sPITarget, $sValue); $this->_aChildren[EasyXML::NODE_PI][] = $oChild; $this->_aChildOrder[] = $oChild; return $oChild; }