Created
July 30, 2018 21:50
-
-
Save birawaich/78ce6852c0e1052c76c2f440e8ab23cf to your computer and use it in GitHub Desktop.
PHP: used to write 1 XML Node with the XMLWriter, based on where an XMLReader is
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* writes one node based on nodetype | |
* | |
* _implementation_ | |
* In an object which has the properties 'xmlReader' and 'xmlWriter' which are instances of the XMLReader/XMLWriter class respectively. | |
* | |
* can be used after each XMLReader::read() | |
* there are 3 groups | |
* - handled types (they get written) | |
* - ignored types (they get purposely ignored) | |
* - unhandled types (they get not written or ignored because I don't know what they are) | |
* | |
* @param void takes input from properties | |
* @return boolean true on sucess, false on failure | |
*/ | |
function writeOneNode() { | |
switch($this->xmlReader->nodeType) { | |
//handled types | |
case XMLReader::NONE: | |
break; | |
case XMLReader::ELEMENT: | |
if(!$this->xmlWriter->startElement($this->xmlReader->name)) {trigger_error('Cold not start Element.',E_USER_WARNING);return false;}; | |
if($this->xmlReader->hasAttributes) { //handle attributes | |
$attCount = $this->xmlReader->attributeCount; | |
for($i = 0; $i < $attCount; $i++) { | |
if(!$this->xmlReader->moveToNextAttribute()){trigger_error('Could not move to next Attribute.',E_USER_WARNING);return false;}; | |
if(!$this->xmlWriter->writeAttribute($this->xmlReader->name, $this->xmlReader->value)) {trigger_error('Could not write Attribute ('.$this->xmlReader->name.'=>'.$this->xmlReader->value.').',E_USER_WARNING);return false;} | |
} | |
if(!$this->xmlReader->moveToElement()) {trigger_error('Could not move back to Element',E_USER_WARNING);return false;} | |
} | |
break; | |
case XMLReader::ATTRIBUTE: | |
if(!$this->xmlWriter->writeAttribute($this->xmlReader->name, $this->xmlReader->getAttribute($this->xmlReader->name))) {trigger_error('Could not Write Attribute ('.$this->xmlReader->name.'=>'.$this->xmlReader->getAttribute($this->xmlReader->name).').',E_USER_WARNING);return false;} | |
break; | |
case XMLReader::TEXT: | |
case XMLReader::SIGNIFICANT_WHITESPACE: | |
if(!$this->xmlWriter->text($this->xmlReader->value)) {trigger_error('Could not write text ('.$this->xmlReader->value.').',E_USER_WARNING);return false;} | |
break; | |
case XMLReader::CDATA: | |
if(!$this->xmlWriter->writeCData($this->xmlReader->value)) {trigger_error('Could not write CDATA ('.$this->xmlReader->value.').',E_USER_WARNING);return false;} | |
break; | |
case XMLReader::END_ELEMENT: | |
if(!$this->xmlWriter->fullEndElement()) {trigger_error('Could not write End Element.',E_USER_WARNING);return false;} //important that it is full element: otherwise it will change up the name types -> wrong levels -> everything is broken | |
break; | |
//igored types | |
case XMLReader::ATTRIBUTE: //will never be reached through XMLReader::read() | |
break; | |
//unhandled types | |
case XMLReader::ENTITY_REF: | |
case XMLReader::ENTITY: | |
case XMLReader::PI: | |
case XMLReader::COMMENT: | |
case XMLReader::DOC: | |
case XMLReader::DOC_TYPE: | |
case XMLReader::DOC_FRAGMENT: | |
case XMLReader::NOTATION: | |
case XMLReader::WHITESPACE: | |
case XMLReader::END_ENTITY: | |
case XMLReader::XML_DECLARATION: | |
case XMLReader::LOADDTD: | |
case XMLReader::DEFAULTATTRS: | |
case XMLReader::VALIDATE: | |
case XMLReader::SUBST_ENTITIES: | |
trigger_error('Came around unhandled Node Type. Will continue execution.'); | |
break; | |
default: //unkown type | |
trigger_error('Came around unkown Node Type ('.(string)$this->xmlReader->nodeType.')', E_USER_WARNING); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment