Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

find xml element by attribute using php

Posted on 2008-06-11
6
Medium Priority
?
5,976 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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‚Ķ
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

636 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