Jump to content

simpleXMLElement attribute parsing.


DIM3NSION

Recommended Posts

I have a simpleXMLelement and the element objects in the array have the attribute [type]. This distinguishes them from each other. How would i go about parsing the objects that just have the attribute type set to [type] => release and displaying the summary section of that object.

 

below is an simpleXMLelement object code as an example

 

    [6] => SimpleXMLElement Object

                        (

                            [@attributes] => Array

                                (

                                    [num] => 7

                                    [type] => master

                                )

 

                            [title] => DJ Tiësto - Sparkles

                            [uri] => http://www.discogs.com/DJ-Tiësto-Sparkles/master/4001

                            [summary] => DJ Tiësto Sparkles Electronic Trance 1999  Sparkles (Airscape Remix) Sparkles (Original) Sparkles

                        )

 

 

 

 

and this is the PHP that creates the element -

 

 

$xmlmusic = new SimpleXMLElement($result);

 

Thanks guys.

DIM3NSION  :D

 

Link to comment
Share on other sites

This is one of the somewhat annoying things about simplexml, but you need to call attributes() on the node, and it returns you an associative array with the keys being the attribute names.  You also have to cast it to whatever variable type it should be in most cases.  I'll do something like this:

 

$t = $xmlmusic->nodename->attributes();
echo (string)$t['type'];

 

 

Link to comment
Share on other sites

Would i have to do a foreach loop on that?

 

foreach ($xmlmusic as $xm)

    {

        $attrs = $xm->attributes();

        if($attrs['type'] == "release")

            echo $xm->summary."\n";

    }

 

Ive tried this but with no luck. Is there anything wrong with that code?

 

Thanks for your help,

 

DIM3NSION

Link to comment
Share on other sites

It might make more sense to use an XPath query to ask for only the type="release" results.

 

$releases = $xmlmusic->xpath('searchresults/result[@type="release"]');
foreach ($releases as $release) {
echo $release->summary . PHP_EOL;
}

 

P.S. The above is based on using the Discogs API, which it looks like you're using.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.