Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created January 16, 2019 13:55
Show Gist options
  • Save kobus1998/3f169e26186ec127f0bf794ead3bafd2 to your computer and use it in GitHub Desktop.
Save kobus1998/3f169e26186ec127f0bf794ead3bafd2 to your computer and use it in GitHub Desktop.
Create xml
<?php
function addXml($name, $input = null, $attrs = [])
{
$xml = "";
$xml .= "<{$name}";
if (!empty($attrs)) {
foreach($attrs as $key => $value) {
$xml .= " {$key}=\"{$value}\"";
}
}
if ($input == null) {
$xml .= "/>";
} else {
$xml .= ">";
$xml .= $input;
$xml .= "</{$name}>";
}
return $xml;
}
// normal node
addXml("name", null, ["id" => 1]); // <name id="1"/>
// closing node
addXml("name", "jan", ["id" => 1]); // <name id="1">jan</name>
// multiple nodes
addXml("Root", addXml("Child", 1), ["xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"]); // <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Child>1</Child></Root>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment