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

FATAL

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