n@1105: axis --> element --> value
n@1105: class nestedObject {
n@1105: function __construct($id) {
n@1105: $this->id = $id;
n@1105: $this->nest = array();
n@1105: $this->type = null;
n@1105: $this->num = 0;
n@1105: }
n@1105: function addNewChild($id) {
n@1105: if ($this->type == null) {
n@1105: $this->type = "nest";
n@1105: }
n@1105: if ($this->type == "nest") {
n@1105: $obj = new nestedObject($id);
n@1105: array_push($this->nest,$obj);
n@1105: $this->num = count($this->nest);
n@1105: return $this->nest[$this->num-1];
n@1105: }
n@1105: return null;
n@1105: }
n@1105: function findChild($checkId) {
n@1105: if ($this->type == "nest"){
n@1105: foreach($this->nest as $child)
n@1105: {
n@1105: if (strcmp($checkId,$child->id) == 0) {
n@1105: return $child;
n@1105: }
n@1105: }
n@1105: }
n@1105: return null;
n@1105: }
n@1105: function addValue($val) {
n@1105: if ($this->type == null) {
n@1105: $this->type = "value";
n@1105: }
n@1105: if ($this->type == "value") {
n@1105: array_push($this->nest,$val);
n@1105: $this->num = count($this->nest);
n@1105: return $this->nest[$this->num-1];
n@1105: }
n@1105: return null;
n@1105: }
n@1105: }
n@1105:
n@1105: // Build the root nest object to hold the testPages
n@1105: $root = new nestedObject("root");
n@1105:
n@1105: // XML Saves location - assumes it will be saves/
n@1105: $saves = glob("../saves/*.xml");
n@1105: if (is_array($saves))
n@1105: {
n@1105: foreach($saves as $filename) {
n@1105: $xml_string = file_get_contents($filename, FILE_TEXT);
n@1105: $xml_object = simplexml_load_string($xml_string);
n@1105: if ($xml_object == false) {
n@1105: echo "
FATAL
could not parse file ".$filename.": ";
n@1105: foreach(libxml_get_errors() as $error) {
n@1105: echo "
", $error->message;
n@1105: }
n@1105: } else {
n@1105: // Iterate over each $page node
n@1105: foreach($xml_object->page as $pageInstance)
n@1105: {
n@1105: // Find in the nest
n@1105: $pageInstanceId = $pageInstance['id'];
n@1105: $page_nest = $root->findChild($pageInstanceId);
n@1105: if ($page_nest == null) {
n@1105: $page_nest = $root->addNewChild($pageInstanceId);
n@1105: }
n@1105:
n@1105: // Iterate over each $element node
n@1105: foreach($pageInstance->audioelement as $element) {
n@1105:
n@1105: // Now get the tags
n@1105: foreach($element->value as $value) {
n@1105: $axis_nest = null;
n@1105: $axisName = "default";
n@1105: if (isset($value['interface-name']))
n@1105: {
n@1105: // Find the axis nest
n@1105: $axisName = $value['interface-name'];
n@1105: }
n@1105:
n@1105: $axis_nest = $page_nest->findChild($axisName);
n@1105: if ($axis_nest == null) {
n@1105: $axis_nest = $page_nest->addNewChild($axisName);
n@1105: }
n@1105:
n@1105: // Find our specific element tag
n@1105: $elementId = $element['id'];
n@1105: $element_nest = $axis_nest->findChild($elementId);
n@1105: if ($element_nest == null) {
n@1105: $element_nest = $axis_nest->addNewChild($elementId);
n@1105: }
n@1105: // Now push our value
n@1105: $element_nest->addValue($value);
n@1105: }
n@1105: }
n@1105: }
n@1105: }
n@1105: }
n@1105: // We now have a structure in $root. EXPORT IT
n@1105: switch($_GET['format']) {
n@1105: case "XML":
n@1105: // Convert to XML
n@1105: $doc_root = new SimpleXMLElement('');
n@1105: foreach($root->nest as $page) {
n@1105: $doc_page = $doc_root->addChild("page");
n@1105: $doc_page->addAttribute("id",$page->id);
n@1105: foreach($page->nest as $axis) {
n@1105: $doc_axis = $doc_page->addChild("interface");
n@1105: $doc_axis->addAttribute("name",$axis->id);
n@1105: foreach($axis->nest as $element) {
n@1105: $doc_element = $doc_axis->addChild("audioelement");
n@1105: $doc_element->addAttribute("id",$element->id);
n@1105: foreach($element->nest as $value) {
n@1105: $doc_value = $doc_element->addChild("value",$value);
n@1105: }
n@1105: }
n@1105: }
n@1105: }
n@1105: echo $doc_root->asXML();
n@1105: break;
n@1105: case "JSON":
n@1105: // Convert to JSON
n@1105: $doc_root = '{ "pages": [';
n@1105: for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++)
n@1105: {
n@1105: $page = $root->nest[$pageIndex];
n@1105: $doc_page = '{ "id": "'.$page->id.'", "axis": [';
n@1105: for($axisIndex = 0; $axisIndex < $page->num; $axisIndex++)
n@1105: {
n@1105: $axis = $page->nest[$axisIndex];
n@1105: $doc_axis = '{ "name": "'.$axis->id.'", "elements": [';
n@1105: for($elementIndex = 0; $elementIndex < $axis->num; $elementIndex++)
n@1105: {
n@1105: $element = $axis->nest[$elementIndex];
n@1105: $doc_element = '{ "id": "'.$element->id.'", "values": [';
n@1105: for ($valueIndex = 0; $valueIndex < $element->num; $valueIndex++)
n@1105: {
n@1105: $doc_element = $doc_element."".strval($element->nest[$valueIndex]);
n@1105: if ($valueIndex < $element->num-1) {
n@1105: $doc_element = $doc_element.', ';
n@1105: }
n@1105: }
n@1105: $doc_element = $doc_element.']}';
n@1105: if ($elementIndex < $axis->num-1) {
n@1105: $doc_element = $doc_element.', ';
n@1105: }
n@1105: $doc_axis = $doc_axis.$doc_element;
n@1105: }
n@1105: $doc_axis = $doc_axis.']}';
n@1105: if ($axisIndex < $page->num-1) {
n@1105: $doc_axis = $doc_axis.', ';
n@1105: }
n@1105: $doc_page = $doc_page.$doc_axis;
n@1105: }
n@1105: $doc_page = $doc_page.']}';
n@1105: if ($pageIndex < $root->num-1) {
n@1105: $doc_page = $doc_page.', ';
n@1105: }
n@1105: $doc_root = $doc_root.$doc_page;
n@1105: }
n@1105: $doc_root = $doc_root.']}';
n@1105: echo $doc_root;
n@1105: break;
n@1105: case "CSV":
n@1105: // Convert to CSV
n@1105: // CSV Columts: page, axis, element, value
n@1105: $doc_string = "page,axis,element,value"."\r\n";
n@1105: foreach($root->nest as $page){
n@1105: foreach($page->nest as $axis) {
n@1105: foreach($axis->nest as $element) {
n@1105: foreach($element->nest as $value) {
n@1105: $doc_string = $doc_string.$page->id.',';
n@1105: $doc_string = $doc_string.$axis->id.',';
n@1105: $doc_string = $doc_string.$element->id.',';
n@1105: $doc_string = $doc_string.$value;
n@1105: $doc_string = $doc_string."\r\n";
n@1105: }
n@1105: }
n@1105: }
n@1105: }
n@1105: echo $doc_string;
n@1105: }
n@1105: } else {
n@1105: echo "FATAL - No saved XML files discovered";
n@1105: }
n@1105:
n@1105: ?>