0

Hey, i have an simpleXMLElement document that features an array of data to do with an artist's albums.

Below is an extract of that XML

  [2] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [num] => 3
                                [type] => artist
                            )

                        [title] => DJ Tiësto
                        [uri] => http://www.discogs.com/artist/DJ+Ti%C3%ABsto
                        [summary] => DJ Tiësto Tijs Michiel Verwest Dutch trance DJ & producer.

In this XML document there are multiple different types of information from artist info to titles of albums. I want to extract certain parts of this data and echo them out. For instance i want to extract the summary's of the array entries that have the [type] defined to artist. I'm guessing i would use a foreach loop that went through all the entries and checked if this was true? Is this the right way to go about it.

I apologise for my confusing explanation

---- EDIT ----

Heres the PHP code that grabs the data -

<?php
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, "http://www.discogs.com/search?type=all&" .
"q=DJ+Tiësto&" . 
"f=xml&" . 
"api_key=<key>");
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_ENCODING, "gzip");
 $result = curl_exec($curl);
 curl_close($curl);


  $xmlmusic = new SimpleXMLElement($result,NULL,true);

 foreach ($xmlmusic as $xm)
    {
    $attrs = $xm->attributes();
    if($attrs["type"] == "title")
        echo $xm->summary."\n";
    }

?>

3

2 Answers 2

1

Well, although the possible duplication here is a very simple example based on your file.

I suppose you have something like this:

<music>
    <dj num="3" type="artist">
        <title>DJ Tiesto</title>
        <uri>http://www.discogs.com/artist/DJ+Ti%C3%ABsto</uri>
        <summary>DJ Tiësto Tijs Michiel Verwest Dutch trance DJ producer.</summary>
    </dj>
    <dj num="4" type="artist">
        <title>title</title>
        <uri>url</uri>
        <summary>summary</summary>
    </dj>

In order to extract the summaries where the attribute type is "title", as you asked, you need something like this:

<?php
    $result = file_get_contents("http://www.discogs.com/search?type=all&" .
               "q=DJ+Tiësto&" . 
               "f=xml&" . 
               "api_key=<key>");

    $xmlmusic = new SimpleXMLElement($result, NULL, false);

    //print_r($xmlmusic);

    foreach ($xmlmusic as $xm)
    {
        $attrs = $xm->attributes();
        if($attrs["type"] == "title")
            echo $xm->summary."\n";
    }
?>

Test for yourself, it works.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi nunaxe your answer seems to be on the right lines, ive tried to implement your code. Ive edited my question with the full php code. Im using cURL however it returns an error of : Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML on the line where you load the xml into the variable xmlmusic
That means simplexml is having a problem parsing the XML. Can you try outputting $result and verifying it's valid XML?
Follow DaOgre advice. Do print_r($result). In my systems calls to cURL and simplexml_load_file() don't work as expected, due to banned inbound info retrieval.
it prints out xml (i believe its xml) an extract -- <resp stat="ok" version="1.0" requests="24"><exactresults><result num="1" type="artist"><title>DJ Tiësto</title><uri>discogs.com/artist/DJ+Ti%C3%ABsto</uri></… end="20" numResults="3279" start="1"><result num="1" type="artist"><title>DJ Tiesto</title><uri>discogs.com/artist/DJ+Tiesto</uri><summary
I edited my answer with a solution that works. Instead of cURL it uses file_get_contents() (it's far simpler) and don't forget to change the last parameter of SimpleXMLElement() to false.
|
0

if you need to get attributes you can use belowe example

$Attributes = $Node->attributes();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.