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@622: $pageInstanceId = $pageInstance['ref'];
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@568: // Find our specific element tag
n@640: $elementId = null;
n@640: if (isset($element['name'])) {
n@640: $elementId = $element['name'];
n@640: } else {
n@640: $elementId = $element['ref'];
n@640: }
n@568: $element_nest = $page_nest->findChild($elementId);
n@568: if ($element_nest == null) {
n@568: $element_nest = $page_nest->addNewChild($elementId);
n@568: }
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@568: $axis_nest = $element_nest->findChild($axisName);
n@523: if ($axis_nest == null) {
n@568: $axis_nest = $element_nest->addNewChild($axisName);
n@523: }
n@523: // Now push our value
n@568: $axis_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@568: foreach($page->nest as $element) {
n@568: $doc_element = $doc_page->addChild("audioelement");
n@568: $doc_element->addAttribute("id",$element->id);
n@568: foreach($element->nest as $axis) {
n@568: $doc_axis = $doc_element->addChild("interface");
n@568: $doc_axis->addAttribute("name",$axis->id);
n@568: foreach($axis->nest as $value) {
n@568: $doc_axis->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@568: $doc_page = '{ "id": "'.$page->id.'", "elements": [';
n@568: for($elementIndex = 0; $elementIndex < $page->num; $elementIndex++)
n@523: {
n@568: $element = $page->nest[$elementIndex];
n@568: $doc_element = '{ "id": "'.$element->id.'", "axis": [';
n@568: for($axisIndex = 0; $axisIndex < $element->num; $axisIndex++)
n@523: {
n@568: $axis = $element->nest[$axisIndex];
n@568: $doc_axis = '{ "name": "'.$axis->id.'", "values": [';
n@568: for ($valueIndex = 0; $valueIndex < $axis->num; $valueIndex++)
n@523: {
n@568: $doc_axis = $doc_axis."".strval($axis->nest[$valueIndex]);
n@568: if ($valueIndex < $axis->num-1) {
n@568: $doc_axis = $doc_axis.', ';
n@523: }
n@523: }
n@568: $doc_axis = $doc_axis.']}';
n@568: if ($axisIndex < $element->num-1) {
n@568: $doc_axis = $doc_axis.', ';
n@523: }
n@568: $doc_element = $doc_element.$doc_axis;
n@523: }
n@568: $doc_element = $doc_element.']}';
n@568: if ($elementIndex < $page->num-1) {
n@568: $doc_element = $doc_element.', ';
n@523: }
n@568: $doc_page = $doc_page.$doc_element;
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@568: $doc_string = "page,element,axis,value"."\r\n";
n@523: foreach($root->nest as $page){
n@568: foreach($page->nest as $element) {
n@568: foreach($element->nest as $axis) {
n@568: foreach($axis->nest as $value) {
n@565: $doc_string = $doc_string.$page->id.',';
n@568: $doc_string = $doc_string.$element->id.',';
n@565: $doc_string = $doc_string.$axis->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: ?>