-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: - test that JSON files converted via PHP validate against the JSON schema - test that JSON files converted via PHP can be reconverted back to XML without loss of information - protect against large XML files - protect against malicous XML files
- Loading branch information
Paolo Greppi
committed
Jul 3, 2018
1 parent
33df0b2
commit 4d4e150
Showing
5 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
vendor | ||
update.sh | ||
.directory |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"squizlabs/php_codesniffer": "*" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
// sample usage: | ||
// curl -X POST -F 'xml=@samples/IT01234567890_FPA01.xml' http://localhost:8000/xml2json.php | ||
|
||
function normalize(&$array, $arrayNotation) | ||
{ | ||
foreach ($array as $key => $value) { | ||
normalize($array[$key], $arrayNotation); | ||
if (in_array($key, $arrayNotation) && is_array($value)) { | ||
$array[$key] = [$array[$key]]; | ||
} | ||
} | ||
} | ||
|
||
$arrayNotation = [ | ||
"FatturaElettronicaBody", | ||
"Causale", | ||
"DatiOrdineAcquisto", | ||
"RiferimentoNumeroLinea", | ||
"DatiContratto", | ||
"DatiConvenzione", | ||
"DatiRicezione", | ||
"DettaglioLinee", | ||
"DatiRiepilogo", | ||
"DatiPagamento" | ||
]; | ||
|
||
$xml = simplexml_load_file($_FILES['xml']['tmp_name']); | ||
// convert back and forth to transform SimpleXMLElement Objects to associative arrays | ||
$json = json_encode($xml); | ||
$array = json_decode($json, true); | ||
// recusively traverse array and convert selected scalar values to arrays | ||
normalize($array, $arrayNotation); | ||
// convert @attributes to keys | ||
foreach ($array['@attributes'] as $key => $value) { | ||
$array[$key] = $value; | ||
} | ||
unset($array['@attributes']); | ||
echo json_encode(array("FatturaElettronica" => $array), JSON_PRETTY_PRINT); |