Created
August 2, 2015 06:38
-
-
Save broject/463f7c3f4b4037032d6b to your computer and use it in GitHub Desktop.
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
<?php | |
if (!defined('BASEPATH')) | |
exit('No direct script access allowed'); | |
/** | |
* b.boldbaatar | |
* | |
* xml tool for array to xml | |
* @author HiimelOyun Dev Team | |
* @link boroo_c@yahoo.com | |
* | |
* | |
* $this->load->library('XmlTool'); | |
$val = json_encode(array('name', array('role' => 'admin'), TRUE)); | |
$prm_array = array( | |
'class' => array( | |
//'["name",{"role":"admin"},true]' | |
$val => 'computer software', | |
'employees' => array( | |
array( | |
'employee' => array( | |
'name' => 'boroo', | |
'tasks' => array( | |
array( | |
'task' => array( | |
'name' => 'com1' | |
) | |
), | |
array( | |
'task' => array( | |
'name' => 'com2', | |
'time' => '3' | |
) | |
), | |
) | |
) | |
), | |
array( | |
'employee' => array( | |
'name' => 'odgii', | |
) | |
) | |
) | |
) | |
); | |
$xml = new HO_XmlTool(); | |
$xml_writer = $xml->start(); | |
$xml->fromArray($prm_array, $xml_writer); | |
if (!isset($this->file)) { | |
$this->load->helper('file'); | |
} | |
$this->output->enable_profiler(false); | |
$xml_str = read_file('data/xml/data.xml'); | |
header('Content-type: text/xml'); | |
echo $xml_str;//$xml->end($xml_writer); | |
* | |
*/ | |
// ------------------------------------------------------------------------ | |
class HO_XmlTool { | |
// read | |
// ----------------------------------------------------------------------- | |
public function read_file($file_url) { | |
$reader = new XMLReader(); | |
if (!$reader->open($file_url)) { | |
die("Failed to open 'data.xml'"); | |
} | |
$output = ''; | |
if ($reader->read()) { | |
$output = $reader->readOuterXml(); | |
} | |
$reader->close(); | |
return $output; | |
} | |
public function load_file($file_url) { | |
$reader = new XMLReader(); | |
if (!$reader->open($file_url)) { | |
die("Failed to open 'data.xml'"); | |
} | |
$node = NULL; | |
while ($reader->read()) { | |
$node = $reader->expand(); | |
if ($node->nodeType == XMLReader::ELEMENT) { | |
break; | |
} | |
} | |
$reader->close(); | |
return $node; | |
} | |
public function load_xml($xml) { | |
$reader = new XMLReader(); | |
if (!$reader->xml($xml)) { | |
die("Failed to open 'data.xml'"); | |
} | |
$node = NULL; | |
while ($reader->read()) { | |
$node = $reader->expand(); | |
if ($node->nodeType == XMLReader::ELEMENT) { | |
break; | |
} | |
} | |
$reader->close(); | |
return $node; | |
} | |
// write | |
// ----------------------------------------------------------------------- | |
public function &start($prm_xsltFilePath = '') { | |
$xml_writer = new XMLWriter(); | |
$xml_writer->openMemory(); | |
$xml_writer->setIndent(true); | |
$xml_writer->setIndentString(' '); | |
$xml_writer->startDocument('1.0', 'UTF-8'); | |
if ($prm_xsltFilePath) { | |
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $prm_xsltFilePath . '"'); | |
} | |
return $xml_writer; | |
} | |
public function end(&$xml_writer) { | |
$xml_writer->endDocument(); | |
return $xml_writer->outputMemory(); | |
} | |
public function fromArray($prm_array, &$xml_writer) { | |
foreach ($prm_array as $index => $element) { | |
if (is_array($element)) { | |
if (!is_int($index)) { | |
$this->_startElement($xml_writer, $index, $element); | |
$this->_endElement($xml_writer); | |
} else { | |
$this->fromArray($element, $xml_writer); | |
} | |
} else { | |
$this->_startElement($xml_writer, $index, $element); | |
$this->_endElement($xml_writer); | |
} | |
} | |
} | |
private function _startElement(&$xml_writer, $name, $value) { | |
$cdata = false; | |
$attrs = array(); | |
$index = json_decode($name); | |
if (is_array($index) && !empty($index)) { | |
$list = (array) $index; | |
foreach ($list as $item) { | |
if (is_bool($item)) { | |
$cdata = $item; | |
} elseif (is_object($item)) { | |
$attrs = (array) $item; | |
} else { | |
$name = $item; | |
} | |
} | |
} | |
$xml_writer->startElement($name); | |
if (count($attrs) > 0) { | |
foreach ($attrs as $attr_name => $attr_value) { | |
$xml_writer->writeAttribute($attr_name, $attr_value); | |
} | |
} | |
if (!is_array($value)) { | |
if ($cdata) { | |
$xml_writer->writeCData($value); | |
} else { | |
if (!is_null($value)) { | |
$xml_writer->text($value); | |
} | |
} | |
} else { | |
$this->fromArray($value, $xml_writer); | |
} | |
} | |
private function _endElement(&$xml_writer) { | |
$xml_writer->endElement(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment