Skip to content

Instantly share code, notes, and snippets.

@OlegLazarenko
Created February 6, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegLazarenko/dc726185552bcc466f94 to your computer and use it in GitHub Desktop.
Save OlegLazarenko/dc726185552bcc466f94 to your computer and use it in GitHub Desktop.
<?php
if (!@$_POST['xml'])
{
?>Текст исходного XML:
<form action="<?=$_SERVER['REQUEST_URI']?>" method="post">
<textarea name="xml" rows="30" cols="90"/></textarea><br>
<input type="submit" value="Конвертировать"/>
</form>
<?php die();
}
function appendChild(DOMElement $root, $name, $value = null)
{
$el = $root->ownerDocument->createElement($name);
$value ? $el->nodeValue = $value : null;
return $root->appendChild($el);
}
$inXml = new DOMDocument();
$inXml->preserveWhiteSpace = false;
$inXml->loadXML($_POST['xml']);
$outXml = new DOMDocument('1.0', 'UTF-8');
$outXml->preserveWhiteSpace = false;
$outXml->formatOutput = true;
$outChildren = $outXml->appendChild($outXml->createElement('children'));
foreach ($inXml->documentElement->childNodes as $oldChild)
{
$oldChild = simplexml_import_dom($oldChild);
$newChild = appendChild($outChildren, 'child');
$name = appendChild($newChild, 'name');
appendChild($name, 'last', $oldChild->ТЕКСТ7);
appendChild($name, 'first', $oldChild->ТЕКСТ8);
appendChild($name, 'middle', $oldChild->ТЕКСТ9);
$dateOfBirth = strtotime($oldChild->ТЕКСТ11);
$dateOfBirth = date('Y-m-d', $dateOfBirth) . 'Z';
appendChild($newChild, 'dateOfBirth', $dateOfBirth);
appendChild($newChild, 'polisSer', $oldChild->ТЕКСТ24);
appendChild($newChild, 'polisNum', $oldChild->ТЕКСТ27);
appendChild($newChild, 'documentSer', $oldChild->ТЕКСТ61);
appendChild($newChild, 'documentNum', $oldChild->ТЕКСТ56);
appendChild($newChild, 'mkb', 'Z00');
}
$outData = $outXml->saveXML();
header('Content-Length:' . strlen($outData));
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"out.xml\"");
echo $outData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment