n@523: axis --> element --> value n@523: class nestedObject { n@523: function __construct($id) { n@523: $this->id = $id; n@523: $this->nest = array(); n@523: $this->type = null; n@523: $this->num = 0; n@523: } n@523: function addNewChild($id) { n@523: if ($this->type == null) { n@523: $this->type = "nest"; n@523: } n@523: if ($this->type == "nest") { n@523: $obj = new nestedObject($id); n@523: array_push($this->nest,$obj); n@523: $this->num = count($this->nest); n@523: return $this->nest[$this->num-1]; n@523: } n@523: return null; n@523: } n@523: function findChild($checkId) { n@523: if ($this->type == "nest"){ n@523: foreach($this->nest as $child) n@523: { n@523: if (strcmp($checkId,$child->id) == 0) { n@523: return $child; n@523: } n@523: } n@523: } n@523: return null; n@523: } n@523: function addValue($val) { n@523: if ($this->type == null) { n@523: $this->type = "value"; n@523: } n@523: if ($this->type == "value") { n@523: array_push($this->nest,$val); n@523: $this->num = count($this->nest); n@523: return $this->nest[$this->num-1]; n@523: } n@523: return null; n@523: } n@523: } n@523: n@523: // Build the root nest object to hold the testPages n@523: $root = new nestedObject("root"); n@523: n@523: // XML Saves location - assumes it will be saves/ n@523: $saves = glob("../saves/*.xml"); n@523: if (is_array($saves)) n@523: { n@523: foreach($saves as $filename) { n@523: $xml_string = file_get_contents($filename, FILE_TEXT); n@523: $xml_object = simplexml_load_string($xml_string); n@523: if ($xml_object == false) { n@523: echo "

FATAL

could not parse file ".$filename.": "; n@523: foreach(libxml_get_errors() as $error) { n@523: echo "
", $error->message; n@523: } n@523: } else { n@523: // Iterate over each $page node n@523: foreach($xml_object->page as $pageInstance) n@523: { n@523: // Find in the nest n@523: $pageInstanceId = $pageInstance['id']; n@523: $page_nest = $root->findChild($pageInstanceId); n@523: if ($page_nest == null) { n@523: $page_nest = $root->addNewChild($pageInstanceId); n@523: } n@523: n@523: // Iterate over each $element node n@523: foreach($pageInstance->audioelement as $element) { n@523: n@523: // Now get the tags n@523: foreach($element->value as $value) { n@523: $axis_nest = null; n@523: $axisName = "default"; n@523: if (isset($value['interface-name'])) n@523: { n@523: // Find the axis nest n@523: $axisName = $value['interface-name']; n@523: } n@523: n@523: $axis_nest = $page_nest->findChild($axisName); n@523: if ($axis_nest == null) { n@523: $axis_nest = $page_nest->addNewChild($axisName); n@523: } n@523: n@523: // Find our specific element tag n@523: $elementId = $element['id']; n@523: $element_nest = $axis_nest->findChild($elementId); n@523: if ($element_nest == null) { n@523: $element_nest = $axis_nest->addNewChild($elementId); n@523: } n@523: // Now push our value n@523: $element_nest->addValue($value); n@523: } n@523: } n@523: } n@523: } n@523: } n@523: // We now have a structure in $root. EXPORT IT n@523: switch($_GET['format']) { n@523: case "XML": n@523: // Convert to XML n@523: $doc_root = new SimpleXMLElement(''); n@523: foreach($root->nest as $page) { n@523: $doc_page = $doc_root->addChild("page"); n@523: $doc_page->addAttribute("id",$page->id); n@523: foreach($page->nest as $axis) { n@523: $doc_axis = $doc_page->addChild("interface"); n@523: $doc_axis->addAttribute("name",$axis->id); n@523: foreach($axis->nest as $element) { n@523: $doc_element = $doc_axis->addChild("audioelement"); n@523: $doc_element->addAttribute("id",$element->id); n@523: foreach($element->nest as $value) { n@523: $doc_value = $doc_element->addChild("value",$value); n@523: } n@523: } n@523: } n@523: } n@523: echo $doc_root->asXML(); n@523: break; n@523: case "JSON": n@523: // Convert to JSON n@523: $doc_root = '{ "pages": ['; n@523: for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++) n@523: { n@523: $page = $root->nest[$pageIndex]; n@523: $doc_page = '{ "id": "'.$page->id.'", "axis": ['; n@523: for($axisIndex = 0; $axisIndex < $page->num; $axisIndex++) n@523: { n@523: $axis = $page->nest[$axisIndex]; n@523: $doc_axis = '{ "name": "'.$axis->id.'", "elements": ['; n@523: for($elementIndex = 0; $elementIndex < $axis->num; $elementIndex++) n@523: { n@523: $element = $axis->nest[$elementIndex]; n@523: $doc_element = '{ "id": "'.$element->id.'", "values": ['; n@523: for ($valueIndex = 0; $valueIndex < $element->num; $valueIndex++) n@523: { n@523: $doc_element = $doc_element."".strval($element->nest[$valueIndex]); n@523: if ($valueIndex < $element->num-1) { n@523: $doc_element = $doc_element.', '; n@523: } n@523: } n@523: $doc_element = $doc_element.']}'; n@523: if ($elementIndex < $axis->num-1) { n@523: $doc_element = $doc_element.', '; n@523: } n@523: $doc_axis = $doc_axis.$doc_element; n@523: } n@523: $doc_axis = $doc_axis.']}'; n@523: if ($axisIndex < $page->num-1) { n@523: $doc_axis = $doc_axis.', '; n@523: } n@523: $doc_page = $doc_page.$doc_axis; n@523: } n@523: $doc_page = $doc_page.']}'; n@523: if ($pageIndex < $root->num-1) { n@523: $doc_page = $doc_page.', '; n@523: } n@523: $doc_root = $doc_root.$doc_page; n@523: } n@523: $doc_root = $doc_root.']}'; n@523: echo $doc_root; n@523: break; n@523: case "CSV": n@523: // Convert to CSV n@523: // CSV Columts: page, axis, element, value n@523: $doc_string = "page,axis,element,value"."\r\n"; n@523: foreach($root->nest as $page){ n@523: foreach($page->nest as $axis) { n@523: foreach($axis->nest as $element) { n@523: foreach($element->nest as $value) { n@523: $doc_string = $doc_string.$page->id; n@523: $doc_string = $doc_string.$axis->id; n@523: $doc_string = $doc_string.$element->id; n@523: $doc_string = $doc_string.$value; n@523: $doc_string = $doc_string."\r\n"; n@523: } n@523: } n@523: } n@523: } n@523: echo $doc_string; n@523: } n@523: } else { n@523: echo "FATAL - No saved XML files discovered"; n@523: } n@523: n@523: ?>