How do I turn an external xml file into an array in kirby?


#1

Ultimately I want to create this xml to array, then call that file to a field in the panel.

lets say my file name is permissions.xml.

What’s the easiest way to convert that to an array.

Thanks


#2

The toolkit XML class has a parse() method…

/** 
   * Parses a XML string and returns an array
   * 
   * @param  string  $xml
   * @return mixed
   */
  public static function parse($xml) {

    $xml = preg_replace('/(<\/?)(\w+):([^>]*>)/', '$1$2$3', $xml);
    $xml = @simplexml_load_string($xml, null, LIBXML_NOENT | LIBXML_NOCDATA);
    $xml = @json_encode($xml);
    $xml = @json_decode($xml, true);
    return (is_array($xml)) ? $xml : false;

  }

Example:

$string = f::read(kirby()->roots()->index() . '/test.xml');
$array = xml::parse($string);
dump($array);

#3

Thanks :slight_smile: This helps.