[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

find xml element by attribute using php

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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