How to list all xml for php

Asked

Viewed 92 times

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 was not successful.

Like I’m doing:

$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 would like to simplify the code so I don’t have to make an array for every $i item.

  • No return. The return action I’m using is echo json_encode($array, JSON_PRETTY_PRINT);

  • How you are setting the content in the variable $history? If possible post a code that can be tested.

  • simplexml_load_string. I will post my Curl that I use to get xml.

1 answer

2


When you use simplexml_load_string or simplexml_load_file, an object of the type is returned SimpleXMLElement. With this object, you cannot capture a particular element with this code $history->MusicHistory.

Like the knot MusicHistory is the root of XML, automatically function simplexml_load_* will access it when returning the aforementioned object.

To obtain the "child" elements of that node, you need to use the function children([$ns]). The function may or may not receive 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);
  • I don’t know how to thank you. Thank you brother, from my heart.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.