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";
    }

?>

DIM3NSION
  • 1,681
  • 5
  • 20
  • 38
  • 1
    You should try it first and save yourself some time. – Galen Apr 29 '11 at 20:26
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Apr 29 '11 at 20:29
  • possible duplicate of [How to access element attributes with SimpleXml](http://stackoverflow.com/questions/4625045/how-to-access-element-attributes-with-simplexml) – Gordon Apr 29 '11 at 20:31

2 Answers2

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.

nunaxe
  • 1,432
  • 2
  • 15
  • 16
  • 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 – DIM3NSION Apr 29 '11 at 21:19
  • That means simplexml is having a problem parsing the XML. Can you try outputting $result and verifying it's valid XML? – DaOgre Apr 29 '11 at 21:24
  • 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. – nunaxe Apr 29 '11 at 21:50
  • it prints out xml (i believe its xml) an extract -- DJ Tiëstohttp://www.discogs.com/artist/DJ+Ti%C3%ABstoDJ Tiestohttp://www.discogs.com/artist/DJ+Tiesto – DIM3NSION Apr 29 '11 at 21:55
  • 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*. – nunaxe Apr 29 '11 at 22:09
  • Ive pasted your code and added my key and it just sends back a blank page. I appreciate your help though :) – DIM3NSION Apr 29 '11 at 23:05
  • What if you try `$result = file_get_contents("http://www.example.com"); `? It should return the index page HTML code. – nunaxe Apr 29 '11 at 23:30
  • tried google - got this error Uncaught exception 'Exception' with message 'String could not be parsed as XML. on the line with new simpleXMLElement – DIM3NSION Apr 29 '11 at 23:59
  • commented it all out bar just that line and it works but obviously not what i want – DIM3NSION Apr 30 '11 at 00:09
  • DIM3NSION, your original question was "XML Loop Statement in PHP". I posted a solution that does what you asked for. This is not a forum. Diagnose your problem carefully and post another question. I'll be willing to help. Hope you understand. – nunaxe Apr 30 '11 at 16:15
0

if you need to get attributes you can use belowe example

$Attributes = $Node->attributes();

Mustafa
  • 140
  • 1
  • 9