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

FATAL

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