Employ a dedicated XML library to write XML files

This commit is contained in:
Joshua Ramon Enslin 2019-09-30 21:18:04 +02:00 committed by Stefan Rohde-Enslin
parent 52878fee92
commit 1eb21bfc54
5 changed files with 66 additions and 45 deletions

View File

@ -169,7 +169,7 @@ function generateHelpTooltip(string $identifier, string $title, string $explica,
function printDOMDocToXML(DOMDocument $xmlDoc) {
return '<?xml version="1.0" encoding="UTF-8"?>' . $xmlDoc->saveXML($xmlDoc->documentElement);
}
/**
@ -183,7 +183,15 @@ function printDOMDocToXML(DOMDocument $xmlDoc) {
*/
function createTextDomElement(DOMDocument $xmlDoc, string $tag, string $content):DOMElement {
$element = $xmlDoc->createElement($tag);
try {
$element = $xmlDoc->createElement($tag);
}
catch (DOMException $e) {
print_r($e);
print_r($tag);
exit;
}
$element->appendChild($xmlDoc->createTextNode($content));
return $element;

View File

@ -1,41 +1,44 @@
<?PHP
function transform($transform)
{
$transform=str_replace(chr(14),"",$transform);
$transform=str_replace('&quot;','"',$transform);
$transform=str_replace('&','&amp;',$transform);
$transform=str_replace('"','&quot;',$transform);
$transform=str_replace('</br>',' ',$transform);
$transform=str_replace('<br />',' ',$transform);
$transform=str_replace('&rsquo;','\'',$transform);
$transform=str_replace(chr(11),' ',$transform);
$transform=str_replace('<','&lt;',$transform);
$transform=str_replace('>','&gt;',$transform);
$transform=str_replace('´','&apos;',$transform);
$transform=str_replace('&sbquo','\'',$transform);
$transform=str_replace('&lsquo','\'',$transform);
$transform=str_replace(chr(96),'\'',$transform);
$transform=str_replace(chr(130),'\'',$transform);
$transform=str_replace(chr(145),'\'',$transform);
$transform=str_replace(chr(146),'\'',$transform);
$transform = str_replace(chr(14), "", $transform);
$transform = str_replace('&quot;', '"', $transform);
$transform = str_replace('&', '&amp;', $transform);
$transform = str_replace('"', '&quot;', $transform);
$transform = str_replace('</br>', ' ', $transform);
$transform = str_replace('<br />', ' ', $transform);
$transform = str_replace('&rsquo;', '\'', $transform);
$transform = str_replace(chr(11), ' ', $transform);
$transform = str_replace('<', '&lt;', $transform);
$transform = str_replace('>', '&gt;', $transform);
$transform = str_replace('´', '&apos;', $transform);
$transform = str_replace('&sbquo', '\'', $transform);
$transform = str_replace('&lsquo', '\'', $transform);
$transform = str_replace(chr(96), '\'', $transform);
$transform = str_replace(chr(130), '\'', $transform);
$transform = str_replace(chr(145), '\'', $transform);
$transform = str_replace(chr(146), '\'', $transform);
return $transform;
}
function tagify($tagify)
{
$tagify=str_replace(' ','_',$tagify);
$tagify=str_replace('/','_',$tagify);
$tagify=str_replace(',','_',$tagify);
$tagify=str_replace('__','_',$tagify);
$tagify=str_replace('Ä','ae',$tagify);
$tagify=str_replace('ä','ae',$tagify);
$tagify=str_replace('Ö','oe',$tagify);
$tagify=str_replace('ö','oe',$tagify);
$tagify=str_replace('Ü','ue',$tagify);
$tagify=str_replace('ü','ue',$tagify);
$tagify=str_replace('ß','ss',$tagify);
$tagify=str_replace(chr(41),'',$tagify);
$tagify=str_replace(chr(40),'',$tagify);
return $tagify;
$tagify = str_replace(' ', '_', $tagify);
$tagify = str_replace('/', '_', $tagify);
$tagify = str_replace(',', '_', $tagify);
$tagify = str_replace('__', '_', $tagify);
$tagify = str_replace('Ä', 'ae', $tagify);
$tagify = str_replace('ä', 'ae', $tagify);
$tagify = str_replace('Ö', 'oe', $tagify);
$tagify = str_replace('ö', 'oe', $tagify);
$tagify = str_replace('Ü', 'ue', $tagify);
$tagify = str_replace('ü', 'ue', $tagify);
$tagify = str_replace('ß', 'ss', $tagify);
$tagify = str_replace(chr(41), '', $tagify);
$tagify = str_replace(chr(40), '', $tagify);
return $tagify;
}

View File

@ -42,32 +42,43 @@ $fp = fopen ($csv_datei, 'r');
$y = 0;
while ($zeile = fgetcsv($fp, 100000, ';')) {
$zeile = str_replace("\xEF\xBB\xBF", '', $zeile);
$y++;
$zieldatei = 'xml/' . $y . '.xml';
if (!$handle = fopen($zieldatei, 'w')) {
echo "Cannot open file ($zieldatei)";exit;
}
$somecontent = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; // für Ungarn ///////////////////////////////////
/*$somecontent='<?xml version="1.0" encoding="ISO-8859-1"?>'."\n"; // normal */
$somecontent .= '<record>' . "\n";
$xmlDoc = new DOMDocument("1.0", "UTF-8");
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
# $xmlDoc->encoding = 'utf-8';
$xmlMainElem = $xmlDoc->createElement("record");
$record_node = $xmlDoc->appendChild($xmlMainElem); //add record element to XML node
# $rss_node->setAttribute("version", "2.0"); //set RSS version
$lineCount = count($zeile);
for ($x = 0; $x < $lineCount; $x++) {
$inhalt[$y][$x] = $zeile[$x];
if ($inhalt[$y][$x] == '') $inhalt[$y][$x] = 'ERSATZ';
$somecontent = $somecontent . '<' . tagify(transform($inhalt[1][$x])) . '>' . (($inhalt[$y][$x])) . '</' . tagify(transform($inhalt[1][$x])) . '>' . "\n"; //für ungarn sonst weg //////////////////////////////////////////////////////////////
$record_node->appendChild(createTextDomElement($xmlDoc, trim($inhalt[1][$x]), trim($inhalt[$y][$x])));
# $somecontent = $somecontent . '<' . tagify(transform($inhalt[1][$x])) . '>' . (($inhalt[$y][$x])) . '</' . tagify(transform($inhalt[1][$x])) . '>' . "\n"; //für ungarn sonst weg //////////////////////////////////////////////////////////////
}
$somecontent .= '</record>';
$somecontent = '<?xml version="1.0" encoding="UTF-8"?>' . $xmlDoc->saveXML($xmlDoc->documentElement);
# $somecontent .= '</record>';
//if ($y>1)
//{
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";exit;
}
// echo "Success, wrote to file ($filename)<br><br>";
//}
fclose($handle);
}
fclose($fp);

View File

@ -1167,6 +1167,7 @@ function getSimpleAvailableFieldEntry(string $nameTL = "", string $explica = "",
"name_human_readable" => $nameTL,
"explica" => $explica,
];
}
/**
@ -1181,7 +1182,7 @@ function getSimpleAvailableFieldEntry(string $nameTL = "", string $explica = "",
* @return array
*/
function generateGenericFullEvent(array $event, array $csvxml_overview, string $typeName, string $type, int $number):array {
return [
"{$type}_when{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['when'], "", ["{$type}_when_sure{$number}"]),
"{$type}_when_sure{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['when'] . " [{$csvxml_overview['certainty']}]", "", ["{$type}_when{$number}"], ["y", "n"]),
@ -1191,8 +1192,7 @@ function generateGenericFullEvent(array $event, array $csvxml_overview, string $
"{$type}_where_sure{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['where'] . " [{$csvxml_overview['certainty']}]", "", ["{$type}_where{$number}"], ["y", "n"]),
"{$type}_annotation{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['classic_annotation_intro'], ""),
];
}
for ($i = 1; $i <= 2; $i++) {

View File

@ -8,7 +8,6 @@ $fileDir = 'xml/';
$zip = new Zip();
//$zip->setComment("Example Zip file.\nCreated on " . date('l jS \of F Y h:i:s A'));
//$zip->addFile("Hello World!", "hello.txt");
if ($handle = opendir($fileDir)) {
/* This is the correct way to loop over the directory. */