Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created April 7, 2021 07:04
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 ZiTAL/d426e7684a09f14472ed9cb9a366a36b to your computer and use it in GitHub Desktop.
Save ZiTAL/d426e7684a09f14472ed9cb9a366a36b to your computer and use it in GitHub Desktop.
php: json/array to xml
<?php
$file = 'https://raw.githubusercontent.com/ZiTAL/flickr-php-cli/master/composer.json';
$file = file_get_contents($file);
$array = json_decode($file, true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$root = $dom->createElement('root');
$dom->appendChild($root);
array2xml($array, $root, $dom);
echo $dom->saveXML();
function array2xml($array, $node, &$dom)
{
foreach($array as $key => $value)
{
if(preg_match("/^[0-9]/", $key))
$key = "node-{$key}";
$key = preg_replace("/[^a-z0-9_\-]+/i", '', $key);
if($key==='')
$key = '_';
$a = $dom->createElement($key);
$node->appendChild($a);
if(!is_array($value))
$a->appendChild($dom->createTextNode($value));
else
array2xml($value, $a, $dom);
}
}
{
"name": "thefox/flickr-cli",
"description": "Upload and download Flickr photos, photo sets, directories via shell.",
"license": "GPL-3.0",
"type": "project",
"keywords": [
"Flickr",
"Upload",
"Download",
"Photo",
"CLI"
],
"homepage": "https://github.com/TheFox/flickr-cli",
"authors": [
{
"name": "Christian Mayer",
"email": "christian@fox21.at",
"homepage": "https://fox21.at"
}
],
"require": {
"php": "^7.0",
"rezzza/flickr": "^1.1",
"symfony/yaml": "^2.3",
"symfony/console": "^3.1",
"symfony/filesystem": "^3.1",
"symfony/finder": "^3.1",
"monolog/monolog": "^1.21",
"guzzlehttp/guzzle": "^3.8",
"lusitanian/oauth": "^0.2",
"rych/bytesize": "^1.0",
"doctrine/dbal": "^2.5"
},
"require-dev": {
"phpstan/phpstan": "^0.7",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
"psr-4": {
"": "src"
}
},
"bin": [
"bin/flickr-cli"
]
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<name>thefox/flickr-cli</name>
<description>Upload and download Flickr photos, photo sets, directories via shell.</description>
<license>GPL-3.0</license>
<type>project</type>
<keywords>
<node-0>Flickr</node-0>
<node-1>Upload</node-1>
<node-2>Download</node-2>
<node-3>Photo</node-3>
<node-4>CLI</node-4>
</keywords>
<homepage>https://github.com/TheFox/flickr-cli</homepage>
<authors>
<node-0>
<name>Christian Mayer</name>
<email>christian@fox21.at</email>
<homepage>https://fox21.at</homepage>
</node-0>
</authors>
<require>
<php>^7.0</php>
<rezzzaflickr>^1.1</rezzzaflickr>
<symfonyyaml>^2.3</symfonyyaml>
<symfonyconsole>^3.1</symfonyconsole>
<symfonyfilesystem>^3.1</symfonyfilesystem>
<symfonyfinder>^3.1</symfonyfinder>
<monologmonolog>^1.21</monologmonolog>
<guzzlehttpguzzle>^3.8</guzzlehttpguzzle>
<lusitanianoauth>^0.2</lusitanianoauth>
<rychbytesize>^1.0</rychbytesize>
<doctrinedbal>^2.5</doctrinedbal>
</require>
<require-dev>
<phpstanphpstan>^0.7</phpstanphpstan>
<squizlabsphp_codesniffer>^3.0</squizlabsphp_codesniffer>
</require-dev>
<autoload>
<psr-4>
<_>src</_>
</psr-4>
</autoload>
<bin>
<node-0>bin/flickr-cli</node-0>
</bin>
</root>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment