Solved

Filter Results from XML in PHP by Attribute

Posted on 2009-05-04
1
276 Views
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"> 
  <artist>
    <releases>
      <release id="63114" status="Accepted" type="Main">
        <title></title>  
        <format></format>  
        <label></label>  
        <year></year>
       <release id="63114" status="Accepted" type="Remix">
        <title></title>  
        <format></format>  
        <label></label>  
        <year></year>    
      </release>  
     </releases>  
   </artist>  
</resp>  
 
// the php I am using to build an array, I want only releases with type "main"
 
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadXML($response);
$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

0
Comment
Question by:aiwazz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

732 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