Filter Results from XML in PHP by Attribute

Posted on 2009-05-04
Last Modified: 2013-11-11
For the life of me I cant figure out how to do this simple thing, new to dealing with xml in php.

I am getting an xml from the discogs api and would like to filter my results by the attribute "type" found in the release part of the xml snippet below.

The xml has types "main" "remix" etc in it for the release field. I would like to build loop the array with ONLY the releases that are of type "main". The php pertinent to the release array is below in the code area.

I am trying all i can with stuff like $type = $rel->item(0)->getAttribute("type"); and then an if in the loop, but it's failing. arg.

Thank you for any assistance!
// the structure of the xml I am getting


<resp stat="ok" version="1.0" requests="1"> 



      <release id="63114" status="Accepted" type="Main">





       <release id="63114" status="Accepted" type="Remix">










// the php I am using to build an array, I want only releases with type "main"


$doc = new DOMDocument('1.0', 'UTF-8');


$rel = $doc->getElementsByTagName("release");


foreach ($rel as $release)


    $title = $release->getElementsByTagName("title")->item(0)->nodeValue;

    $format = $release->getElementsByTagName("format")->item(0)->nodeValue;

    $label = $release->getElementsByTagName("label")->item(0)->nodeValue;

    $year = $release->getElementsByTagName("year")->item(0)->nodeValue;


    $releases[$title] = array ("title" => $title,

                           "format" => $format,

                           "label" => $label,

                           "year" => $year,



Open in new window

Question by:aiwazz
1 Comment

Accepted Solution

aiwazz earned 0 total points
ID: 24299190
I am dork. solved with adding below in foreach and if in output loop.
$type = $release->getAttribute("type");

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access XML Export Query Setup Multiple Tag Values 15 28
mysqli 3 19
Checking if varaible is empty 6 29
Why my select dropdown does not work? 8 25
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now