Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simpleXML accessing attributes

Posted on 2008-10-06
5
Medium Priority
?
1,393 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
[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
  • 3
5 Comments
 
LVL 14

Accepted Solution

by:
Ali Kayahan earned 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

704 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