?
Solved

find xml element by attribute using php

Posted on 2008-06-11
6
Medium Priority
?
5,940 Views
Last Modified: 2008-06-22
Hi.
I'm trying to read an xml file and replace a specific element data by using attribute id.

that's the xml structure:
<simpleviewerGallery>
<image id="4">
  <filename>ani4.swf</filename>
  <proName>Trake jeep8</proName>
  <upLine>PRODUCT #123543</upLine>
  <downLine>AGE 5 yrs, up</downLine>
  <textFileToload>Trake jeep8.txt</textFileToload>
  <flvFileToload>auto.flv</flvFileToload>
  <bigPic>j0331791.jpg</bigPic>
  </image>
</simpleviewerGallery>

The following code always replace the first element and not the one i need!
How should i fix it?

Thanks



$xml=simplexml_load_file('../pic/'.$galleryName.'/pic.xml');
foreach ($xml->image as $image) {
  if ($xml->image->attributes("id")==$id){
     $xml->image->proName=$proName;
     $xml->image->upLine=$upLine;
     $xml->image->downLine=$downLine;
     if ($video)
        $xml->image->flvFileToload=$video;
     if ($picture)
        $xml->image->bigPic=$picture;
     $xml->image->textFileToload=$proName.".txt";
     break;
  }

Open in new window

0
Comment
Question by:tzvio
[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
6 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 21786749
Try altering the code like this

$id = 4
foreach ($xml->image as $image) {
  if ($image["id"]==$id){
     $xml->image->proName=$proName;
     $xml->image->upLine=$upLine;
     $xml->image->downLine=$downLine;
     if ($video)
        $xml->image->flvFileToload=$video;
     if ($picture)
        $xml->image->bigPic=$picture;
     $xml->image->textFileToload=$proName.".txt";
     break;
  }
}  
?>


The ID attribute is returned in an array and as such needs [] rather than ()

There is an additional problem you should be aware of. If you are using PHP4 then the FOREACH will duplicate each piece of data and updating the duplicate has no effect. See here http://uk.php.net/foreach

(See joaohbruni's comment) -----

My original code only worked in PHP5:

foreach($array as $element) {
  $element->property = "new_value";
}

Solution for both PHP4 and PHP5:

reset($array);
while (list($key, $value) = each($array)) {
  $element =& $array[$key];
  $element->property = "new_value";
}
0
 

Author Comment

by:tzvio
ID: 21787636
i don't need that code anymore
thanks anyway
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

800 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