Solved

simpleXML accessing attributes

Posted on 2008-10-06
5
1,387 Views
Last Modified: 2012-05-05
Hello,

I'm developing a site using simpleXML and so far have not had any problems. However I now need to access an attribute.

My normal way of accessing the XML is like this;

$xml = simplexml_load_file("xml/whats-on-blog.xml");

foreach($xml->gallery as $entry) {
 
}

I've now got to a point where I need to access some tag's attributes. My XML is as below. I would like to be able to change my foreach loop so that it only loops through image <items> with a gallery attribute of 1

How can this be done?
<?xml version="1.0" encoding="utf-8"?>
<galleries>
 
  <gallery id="1">
    <title>Sample Title 1</title>
  </gallery>
 
  <gallery id="2">
    <title>Sample Title 2</title>
  </gallery>
 
  <image gallery="1" id="1" sort="0">
    <src>image1.jpg</src>
  </image>
 
  <image gallery="1" id="2" sort="5">
    <src>image1.jpg</src>
  </image>
 
  <image gallery="1" id="3" sort="10">
    <src>image1.jpg</src>
  </image>
 
  <image gallery="2" id="4" sort="15">
    <src>image1.jpg</src>
  </image>
 
</galleries>

Open in new window

0
Comment
Question by:Mr_Splash
  • 3
5 Comments
 
LVL 14

Accepted Solution

by:
ali_kayahan earned 500 total points
ID: 22648455
    $entry['id']
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 22648519
Thanks for your reply, however that's returning everything between the <galleries> tags.
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 22648548
Sorry, I used $entry['1'] I thought you literally meant replace with id.

I've changed it to foreach($xml as $entry['id']) However it's still not working.
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 22648566
Ok, figured this out.
foreach($xml as $entry) {
  $gallery = $entry['gallery'];
  if ($gallery == 1) {
    echo $entry->src.'<br />';
  }
}

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 22648571
Try this code:
foreach($xml->image as $entry) {
  echo $entry{'id'};
 
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Grouping 2 41
google map tracking website 5 23
Download tables into separate sheets 3 27
PHP AJAX JSON 2 60
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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.

776 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