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

@ -183,7 +183,15 @@ function printDOMDocToXML(DOMDocument $xmlDoc) {
*/ */
function createTextDomElement(DOMDocument $xmlDoc, string $tag, string $content):DOMElement { function createTextDomElement(DOMDocument $xmlDoc, string $tag, string $content):DOMElement {
try {
$element = $xmlDoc->createElement($tag); $element = $xmlDoc->createElement($tag);
}
catch (DOMException $e) {
print_r($e);
print_r($tag);
exit;
}
$element->appendChild($xmlDoc->createTextNode($content)); $element->appendChild($xmlDoc->createTextNode($content));
return $element; return $element;

View File

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

View File

@ -42,32 +42,43 @@ $fp = fopen ($csv_datei, 'r');
$y = 0; $y = 0;
while ($zeile = fgetcsv($fp, 100000, ';')) { while ($zeile = fgetcsv($fp, 100000, ';')) {
$zeile = str_replace("\xEF\xBB\xBF", '', $zeile);
$y++; $y++;
$zieldatei = 'xml/' . $y . '.xml'; $zieldatei = 'xml/' . $y . '.xml';
if (!$handle = fopen($zieldatei, 'w')) { if (!$handle = fopen($zieldatei, 'w')) {
echo "Cannot open file ($zieldatei)";exit; 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 */ $xmlDoc = new DOMDocument("1.0", "UTF-8");
$somecontent .= '<record>' . "\n"; $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); $lineCount = count($zeile);
for ($x = 0; $x < $lineCount; $x++) { for ($x = 0; $x < $lineCount; $x++) {
$inhalt[$y][$x] = $zeile[$x]; $inhalt[$y][$x] = $zeile[$x];
if ($inhalt[$y][$x] == '') $inhalt[$y][$x] = 'ERSATZ'; 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 ($y>1)
//{ //{
if (fwrite($handle, $somecontent) === FALSE) { if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";exit; echo "Cannot write to file ($filename)";exit;
} }
// echo "Success, wrote to file ($filename)<br><br>";
//}
fclose($handle); fclose($handle);
} }
fclose($fp); fclose($fp);

View File

@ -1167,6 +1167,7 @@ function getSimpleAvailableFieldEntry(string $nameTL = "", string $explica = "",
"name_human_readable" => $nameTL, "name_human_readable" => $nameTL,
"explica" => $explica, "explica" => $explica,
]; ];
} }
/** /**
@ -1192,7 +1193,6 @@ function generateGenericFullEvent(array $event, array $csvxml_overview, string $
"{$type}_annotation{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['classic_annotation_intro'], ""), "{$type}_annotation{$number}" => getSimpleAvailableFieldEntry($typeName . ": " . $event['classic_annotation_intro'], ""),
]; ];
} }
for ($i = 1; $i <= 2; $i++) { for ($i = 1; $i <= 2; $i++) {

View File

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