$obj = rtrim($obj, ", ") . " ],"; /* add the answer */ $obj .= "\n{$tab3}{$quote}" . "answer{$quote}: {$quote}{$questionAnswer}{$quote}" . "\n{$tab2}},"; } $obj = rtrim($obj, ","); $obj .= "\n{$tab}]" . "\n}"; $fh = fopen("../data/review01.json", "w"); $err = fwrite($fh, $obj); fclose($fh); return $err; } function displayConfirmationMessage($message) { echo <<<_HD \t\t<!DOCTYPE html> \t\t<html> \t\t<head> \t\t<title>Save Status</title> \t\t<link rel="stylesheet" href="../css/site.css"> \t\t</head> \t\t<body> \t\t<div id="save-status-message">{$message}</div> \t\t</body> \t\t</html> _HD; } if (writeJSONFile() !== false) { displayConfirmationMessage("JSON file created successfuly."); } else { displayConfirmationMessage("Unable to create JSON file"); }
} } if (!isset($argv[1]) || !isset($argv[2])) { throw new Exception('Please pass in two arguments: the input .tmx file and the output .json file.'); } $inputTMXFile = $argv[1]; $outputJSONFile = $argv[2]; $doc = new DOMDocument('1.0', 'ISO-8859-1'); $handle = fopen($inputTMXFile, 'r'); if (!$doc->loadXML(fread($handle, filesize($inputTMXFile)))) { throw new Exception('Error reading master.tmx file.'); } $json = array(); $tus = $doc->getElementsByTagName('tu'); foreach ($tus as $tu) { $obj = new stdClass(); if ($tu->hasAttribute('tuid')) { $obj->variable = createVariableName($tu->getAttribute('tuid')); $obj->en = $tu->getAttribute('tuid'); $currentTuv = $tu->firstChild; if ($tu->firstChild != null && $tu->firstChild->firstChild != null) { if ($tu->firstChild->hasAttribute('xml:lang')) { $lang = $tu->firstChild->getAttribute('xml:lang'); $obj->{$lang} = $tu->firstChild->firstChild->textContent; } } } $json[] = str_replace('":"', '": "', str_replace('"}', "\"\n}", str_replace('{"', "{\n\t\"", str_replace('",', "\",\n\t", json_encode($obj, JSON_UNESCAPED_UNICODE))))); } writeJSONFile($outputJSONFile, "[" . join(",", $json) . "]");