Salut,
Je vais répondre à côté. Mais peut être que ce genre de choses t'intéresse. Comme je ne fais plus de php. Je ne maintiens plus cette lib que j'ai faite il y a un petit moment :
l'arborescence :
index.php
<?php
$files = array_filter(scandir('.'), function($item){
return strstr($item, 'example');
});
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach($files as $file): ?>
<li><a href="./<?= $file ?>"><?= substr($file, 0, strrpos($file, '.')) ?></a></li>
<?php endforeach ?>
</ul>
</body>
</html>
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<nodes>
</nodes>
exemple buildtree
<?php
header('Content-Type: text/xml; charset=utf-8');
require_once './libs/XMLWriterDom.php';
$xml = new XMLWriterDom();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->load('./data.xml');
$root = $xml->documentElement;
$dom = [
"os" => [
'gnu_linux' => [
'arch?test=false' => '',
'debian?paying=false' => [
'lenny' => '2009',
'Squeeze' => '2011',
'Wheezy' => '2013',
'Jessie' => '2015',
'Stretch' => '2017',
'fork' => [
'ubuntu' => [
'HardyHeron' => '2008',
'lucidlynx?best=true' => '2010',
'PrecisePangolin' => '2012',
'fork' => [
'elementary_os' => [
'Luna' => '2013',
'Loki' => '2015',
]
]
]
]
],
'slakware' => [
'thirteen' => '2009',
'fourteen' => '2012',
'fork' => [
'suse?best=true&t=i+love+you' => [
'six' => '2000',
'eight' => '2001',
]
]
],
'redhat?test=false' => '',
],
'windows?state=bad&virus=true&t=i+hate+this+one' => [
'version_9x' => [
'ninetyfive' => '1995',
'ninetyeight' => '1998',
'ninetyeightse' => '1998',
],
'nt' => [
'nt97' => '1997',
'nt4' => '2000',
'xp' => '2001',
'vista' => '2007',
'seven' => '2009',
'eight' => '2009',
'ten' => '2014',
]
]
]
];
$xml->buildTree($root, $dom);
echo $xml->saveXML();
?>
exemple wrap build tree
<?php
header('Content-Type: text/xml; charset=utf-8');
require_once './libs/XMLWriterDom.php';
$xml = new XMLWriterDom();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->load('./data.xml');
$root = $xml->documentElement;
$dom = [
[
"title" => "Rear Window",
"director" => "Alfred Hitchcock",
"year" => '1954',
],
[
"title" => "Full Metal Jacket",
"director" => "Stanley Kubrick",
"year" => '1987',
"actors" => [
'joker' => [
'firstname' => 'Matthew',
'lastname' => 'Modine'
],
'cowboy' => [
'firstname' => 'Arliss',
'lastname' => 'Howard'
],
'baleine' => [
'firstname' => 'Vincent',
'lastname' => "D'Onofrio"
],
]
],
[
"title" => "Mean Streets",
"director" => "Martin Scorsese",
"year" => '1973'
],
];
foreach($dom as $key => $value){
$node = $xml->wrapBuildTree($value,'movie');
$root->appendChild($node);
}
echo $xml->display();
?>
XMLWriterDOM
<?php
class XMLWriterDom extends \DomDocument{
protected $XMLSource = null;
protected $XMLDestination = null;
protected $root = null;
public function __construct()
{
parent::__construct();
}
public function createSimpleElement($el, $txt = "", $attributes = []){
$node = $this->createElement($el);
$this->writeAttributes($node, $attributes);
if(!empty(trim($txt))){
$txt = $this->createTextNode($txt);
$node->appendChild($txt);
}
return $node;
}
public function wrapBuildTree($data, $txtElementWrapper, $attributes = []){
$wrap = $this->createElement($txtElementWrapper);
$this->writeAttributes($wrap, $attributes);
foreach ($data as $key => $value) {
$attributes = [];
if(strstr($key, '?')){
list($key, $queryString) = explode('?', $key);
parse_str($queryString, $attributes);
}
$item = $this->typeNode($key, $value, $attributes);
$wrap->appendChild($item);
}
return $wrap;
}
public function buildTree($root, $data, $attributes = []){
foreach ($data as $key => $value) {
$attributes = [];
if(strstr($key, '?')){
list($key, $queryString) = explode('?', $key);
parse_str($queryString, $attributes);
}
$wrap = $this->createElement($key);
$this->writeAttributes($wrap, $attributes);
$item = $this->typeNode($key, $value, $attributes);
$node = $wrap->appendChild($item);
$root->appendChild($node);
}
}
protected function writeAttributes($wrap, $attributes){
if(!empty($attributes)){
foreach ($attributes as $attrKey => $attrValue) {
$wrap->setAttribute($attrKey, $attrValue);
}
}
return $wrap;
}
protected function typeNode($key, $value, $attributes){
if(is_array($value)){
$node = $this->wrapBuildTree($value, $key, $attributes);
}
if(is_string($value)){
$node = $this->createSimpleElement($key, $value, $attributes);
}
return $node;
}
/**
* display xml
* @return string [xml state]
*/
public function display(){
return $this->saveXML();
}
/**
* write xml file
* @param string $path
* @return void
*/
public function save($path = null, $option = 0){
$path = !is_null($this->XMLDestination)? $this->XMLDestination : $path;
$this->save($path, $options);
}
/**
* where is xml file
* @param string $value
* @return $this
*/
public function setXMLSource($value){
if(!is_string($value)){
throw new \Exception(sprintf("this args (%s) must be a string", $value));
}
$this->XMLSource = $value;
$this->getXML();
return $this;
}
/**
* where does put xml file
* @param string $value
* @return $this
*/
public function setXMLDestination($value){
if(!is_string($value)){
throw new \Exception(sprintf("this args (%s) must be a string", $value));
}
$this->XMLDestination = $value;
return $this;
}
public function getRoot(){
return $this->root;
}
/**
* search xml file and set attribute root
* @return $this
*/
protected function getXML(){
if(strstr($this->XMLSource, 'http') && !strstr(get_headers($this->XMLSource)[0], '200')){
throw new \Exception(sprintf("%s isn't found", $this->XMLSource));
}else if(!file_get_contents($this->XMLSource)){
throw new \Exception(sprintf("%s doesn't exist", $this->XMLSource));
}
$this->load($this->XMLSource);
$this->root = $this->documentElement;
return $this;
}
}
?>