0

I have a XML file, one part of the file is

<Images>
     <image_>image1.jpg</image_>
     <image_>image2.jpg</image_>
</Images>

I need the image names. I use the code like

$Images = $domtree->getElementsByTagName('Images');
foreach($Images as $Image){


    $Image = $Image->nodeValue."<br>";

    echo $Image;

    }

This is returns the imaged name but at a time as a string, I need as an array. I mean I want to insert the images in the database. Some one help me.

4
  • What do you need help with? Just put the $Image->nodeValues into an array, and then into the database. Commented Aug 28, 2012 at 14:45
  • I put the value in an array get Array ( [0] => image1.jpg image2.jpg ). I need Array ( [0] => image1.jpg,[1] => image2.jpg ) Commented Aug 28, 2012 at 14:50
  • How are you making that array? Commented Aug 28, 2012 at 14:53
  • $val = array(); foreach($Images as $Image){ $Image = $Image->nodeValue; echo $Image; array_push($val,$Image); } Commented Aug 28, 2012 at 14:54

1 Answer 1

1

The problem is you are looping over the Images element(s). So, when you echo nodeValue, you are getting the value of the entire Images element (and all its children).

You need to loop over each of the image_ elements (children) inside the Images element (parent)

$val = array();
$Images = $DOM->getElementsByTagName('Images');
foreach($Images as $Image){
    $imgs = $Image->getElementsByTagName('image_');
    foreach($imgs as $i){
        $img = $i->nodeValue;
        $val[] = $img;
        echo $img."<br>";
    }
}
var_dump($val);
Sign up to request clarification or add additional context in comments.

1 Comment

I have got the currect ans. Thank you for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.