Created
February 25, 2016 07:23
-
-
Save forethoughtde/2c2b7d18b4523a4fb121 to your computer and use it in GitHub Desktop.
XML Parser
This file contains hidden or 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
<?php | |
require('XML.php'); | |
class OpenXML extends XML | |
{ | |
var $fileHandler; | |
public function openXMLFile($filename, $mode='r'){ | |
$this->fileHandler = @fopen($filename, $mode); | |
} | |
public function getHandler() | |
{ | |
return $this->fileHandler; | |
} | |
public function getHandlerType(){ | |
return get_resource_type($this->fileHandler); | |
} | |
} | |
?> | |
This file contains hidden or 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
<?php | |
require('ReadXML.php'); | |
class ParseXML extends ReadXML | |
{ | |
public $handle; | |
public $current_line; | |
public $isTagName = False; | |
public $isTagContent = False; | |
public $startParse = False; | |
public $tagName; | |
public $tagContent; | |
public $tree = array(); | |
//Call appropriate function depend on $isTagName and $isTagContent | |
public function scanCharacter($char, $index) | |
{ | |
if(strcmp($char, '<')==0) | |
{ | |
if($this->isTagContent){ | |
$this->isTagContent = False; | |
} | |
if($index+1 < $this->getLen()) | |
{ | |
$this->isTagName = $this->tagNameOn($this->current_line[$index+1]); | |
} | |
} | |
elseif($this->isTagName and (strcmp($char, '>')==0)) | |
{ | |
$this->isTagName = $this->tagNameOff(); | |
$this->isTagContent = $this->tagContentOn(); | |
} | |
elseif((strcmp($char, '/')==0)){ | |
$this->isTagName = False; | |
$this->isTagContent = False; | |
} | |
elseif($this->isTagName and !strcmp($char, ' ')) | |
{ | |
$this->isTagName = $this->tagNameOff(); | |
} | |
elseif($this->isTagName) | |
{ | |
$this->gatherTagName($char); | |
} | |
elseif($this->startParse and $this->isTagContent) | |
{ | |
$this->gatherTagContent($char); | |
} | |
} | |
public function tagNameOn($nextChar) | |
{ | |
if(!strcmp($nextChar, '/')==0) | |
{ | |
$this->tagName = ""; | |
return True; | |
} | |
// print_r($this->tagName); | |
// print_r("\n"); | |
// print_r($this->tagContent); | |
$this->isTagContent = False; | |
return False; | |
} | |
public function tagNameOff() | |
{ | |
if(!($this->startParse) and strcmp($this->tagName, 'Document') == 0){ | |
$this->startParse = True; | |
} | |
return False; | |
} | |
public function getLen(){ | |
return strlen($this->current_line); | |
} | |
public function tagContentOn() | |
{ | |
return True; | |
} | |
public function gatherTagName($char){ | |
$this->tagName.= $char; | |
} | |
public function gatherTagContent($char){ | |
$this->tagContent.= $char; | |
} | |
public function getTag() | |
{ | |
$this->tagContent = trim($this->tagContent); | |
if($this->tagName and $this->tagContent){ | |
if(!$this->isTagContent){ | |
array_push($this->tree, array($this->tagName, $this->tagContent)); | |
$this->tagContent = ""; | |
$this->tagName = ""; | |
} | |
} | |
} | |
public function getTagContent(){ | |
$listNames = array(); | |
forEach($this->tree as $value){ | |
array_push($listNames, $value[1]); | |
} | |
return $listNames; | |
} | |
public function getTagNames(){ | |
$listNames = array(); | |
forEach($this->tree as $value){ | |
array_push($listNames, $value[0]); | |
} | |
return $listNames; | |
} | |
public function readLine() | |
{ | |
$this->handle = $this->getHandle(); | |
$this->current_line = fgets($this->handle); | |
while($this->current_line) | |
{ | |
for ($i = 0; $i < $this->getLen(); $i++) | |
{ | |
$char = $this->current_line[$i]; | |
$this->scanCharacter($char, $i); | |
} | |
$this->getTag(); | |
} | |
return $this->tree; | |
} | |
public function goOverLine(){ | |
} | |
} | |
$a = new ParseXML('../IMQ+AZIBPrototyp.xml'); | |
$a->openXMLFile($a->getFileName()); | |
$a->acquireHandler(); | |
$a->readLine(); | |
print_r($a->getTagContent()); | |
?> |
This file contains hidden or 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
<?php | |
require('OpenXML.php'); | |
class ReadXML extends OpenXML | |
{ | |
var $content; | |
var $line; | |
public function acquireHandler() | |
{ | |
$this->content = $this->getHandler(); | |
} | |
public function getHandle() | |
{ | |
return $this->content; | |
} | |
public function isReadLine() | |
{ | |
return feof($this->content); | |
} | |
public function getLine() | |
{ | |
if(!($this->isReadLine())) | |
{ | |
$this->line = fgets($this->content); | |
return $this->line; | |
} | |
} | |
} | |
?> |
This file contains hidden or 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
<?php | |
class XML | |
{ | |
var $filename; | |
public function __construct($filename){ | |
$this->filename = $filename; | |
} | |
public function setFileName($filename){ | |
$this->filename = $filename; | |
} | |
public function getFileName(){ | |
return $this->filename; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment