How to do list all of xml to php

0

Good evening. I have the following xml file:

<MusicHistory CompactMode="1">
<Item0 Block="2018-02-11T23:00:00" Folder="Musicas" Music="Zara Larsson - So Good" Composer="" Comment="" Start="2018-11-03T02:19:13.000Z"/>
<Item1 Block="2018-02-11T23:00:00" Folder="Musicas" Music="The Weeknd - Pray For Me" Composer="" Comment="" Start="2018-11-03T02:15:48.000Z"/>
<Item2 Block="2018-02-11T23:00:00" Folder="Musicas" Music="Shawn Mendes - Treat You Better" Composer="" Comment="" Start="2018-11-03T02:12:49.000Z"/>
<Item3 Block="2018-02-11T22:00:00" Folder="Musicas" Music="James Arthur - Say You Won't Let Go" Composer="" Comment="" Start="2018-11-03T02:09:27.000Z"/>
<Item4 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Tove Lo - Cool Girl" Composer="" Comment="" Start="2018-11-03T02:06:12.000Z"/>
<Item5 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Avicii - Taste The Feeling" Composer="" Comment="" Start="2018-11-03T02:03:06.000Z"/>
<Item6 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Jason Derulo - Tiptoe" Composer="" Comment="" Start="2018-11-03T01:59:58.000Z"/>
<Item7 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Little Mix - Love Me Like You" Composer="" Comment="" Start="2018-11-03T01:56:43.000Z"/>
<Item8 Block="2018-02-11T22:00:00" Folder="Musicas" Music="David Gueta - Play Hard" Composer="" Comment="" Start="2018-11-03T01:53:25.000Z"/>
<Item9 Block="2018-02-11T22:00:00" Folder="Musicas" Music="OneRepublic - Apoligize" Composer="" Comment="13+6319C8200604F0C2D46E050A7FC26FE1+10221800" Start="2018-11-03T01:50:14.000Z"/>
<Item10 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Katy Perry - Teenage Dream" Composer="" Comment="" Start="2018-11-03T01:46:34.000Z"/>
<Item11 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Bebe Rexha – No Broken Hearts" Composer="" Comment="" Start="2018-11-03T01:42:38.000Z"/>
<Item12 Block="2018-02-11T22:00:00" Folder="Musicas" Music="MC G15 - Deu Onda" Composer="" Comment="" Start="2018-11-03T01:39:27.000Z"/>
<Item13 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Maluma - Sín Contrato" Composer="" Comment="" Start="2018-11-03T01:35:53.000Z"/>
<Item14 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Pitbull - Options" Composer="" Comment="" Start="2018-11-03T01:31:56.000Z"/>
<Item15 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Rihanna - Umbrella" Composer="" Comment="" Start="2018-11-03T01:27:48.000Z"/>
<Item16 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Skillet - Famous" Composer="" Comment="" Start="2018-11-03T01:24:33.000Z"/>
<Item17 Block="2018-02-11T22:00:00" Folder="Musicas" Music="The Vamps - All Night" Composer="" Comment="" Start="2018-11-03T01:21:26.000Z"/>
<Item18 Block="2018-02-11T22:00:00" Folder="Musicas" Music="OMI - Cheerleader" Composer="" Comment="" Start="2018-11-03T01:18:27.000Z"/>
<Item19 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Austin Mahone - MMM Yeah" Composer="" Comment="" Start="2018-11-03T01:14:38.000Z"/>
<Item20 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Demi Lovato - Stone Cold" Composer="" Comment="" Start="2018-11-03T01:11:36.000Z"/>
</MusicHistory>

I'm trying to list each of this item in php, but I did not succeed.

How do I do:

$am = curl_init();  
curl_setopt($am, CURLOPT_URL, 'file:///C:/Playlist/pgm/Montagem/MusicHistory.xml');
curl_setopt($am, CURLOPT_RETURNTRANSFER, true);
$music = curl_exec($am);

curl_close($am);

$history = simplexml_load_string($music);

$i = 0;
foreach($history->MusicHistory as $listar){
    $data = array(
        'musica' => $listar->Item0[0]->attributes()['Music'], 
        'numero' => $i
    );
    echo json_encode($array, JSON_PRETTY_PRINT);
    $i++;
}

The problem is that the foreach does not return anything. Note: outside the foreach I can get the data normally. For example: $ history-> Item0 [0] -> attributes () ['Music'].

But I'd like to simplify the code so I do not have to make an array for each $ i item.

    
asked by anonymous 03.11.2018 / 03:25

1 answer

2

When you use simplexml_load_string or simplexml_load_file , an object of type SimpleXMLElement is returned. With this object, you can not capture access to a given element with this $history->MusicHistory code.

As the MusicHistory node is the XML root, automatically the simplexml_load_* function will access it when returning the above object.

To get the "child" elements of this node, you must use the children([$ns]) function. The function may or may not be given a parameter (node name).

Example:

<?php

/**
 * No seu caso, basta utilizar 'simplexml_load_string'
 * Utilizei 'simplexml_load_file' para ficar mais fácil de testar
 * $registry = simplexml_load_string($xml);
 */
$registry = simplexml_load_file("data.xml");

/* Define o array que receberá os dados */
$arr = [];

/**
 * Acessa a função 'children' sem passar o nome do nó
 * Dessa forma ela retornará um array de 'SimpleXMLElement'
 * '$key' recebe o índice do array e '$value' o objeto
 */
foreach($registry->children() as $key => $value) {
    $arr[] = [
        "musica" => reset($value['Music']), // Acessa e retorna o primeiro registro do atributo "Music"
        "registro" => $key
    ];
}

/* Imprime na tela o JSON */
echo json_encode($arr, JSON_PRETTY_PRINT);
    
03.11.2018 / 03:53