Created
January 16, 2019 13:55
-
-
Save kobus1998/3f169e26186ec127f0bf794ead3bafd2 to your computer and use it in GitHub Desktop.
Create xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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