Skip to content

Instantly share code, notes, and snippets.

View matt-o-matic's full-sized avatar

P. Matthew Bradford matt-o-matic

View GitHub Profile
@matb33
matb33 / DOMDocumentExtended.php
Last active May 23, 2020 21:26
DOMDocument->saveJSON()
<?php
// saveJSON: XML to JSON conversion/transformation
// The saveJSON method is implemented as a method of the example DOMDocumentExtended class, which
// extends DOMDocument. It will transform the currently loaded DOM into JSON using XSLT. Since
// there isn't a reliable automatic way of detecting if certain siblings nodes should be
// represented as arrays or not, a "forceArray" parameter can be passed to the saveJSON method.
// It should be noted that the forceArray functionality doesn't recognize namespaces. This is
// an easy enough fix, I just didn't need it at the time of writing (look for local-name() and