Sanjay Rathod
Sanjay Rathod

Reputation: 1143

how to display image from xml through php?

i have two files note.xml and xml.php

here is my code of note.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
    <agent>
    <image> img/primary-nav-logo.png</image>
    <name>Tommy Jenkin</name>
    <company>CJenkins Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
    <agent>
    <image> img/primary-nav-logo.png</image>
    <name>Tommy Jenkin</name>
    <company>CJenkins Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
</agents>

and here is my code of xml.php

<?php
                $xml = simplexml_load_file("note.xml") 
                or die("Error: Cannot create object");
                function processXML($node){
                    foreach($node->children() as $agent => $data){
                    if(trim($data) != ""){
                        echo $data;
                        echo "</br>";
                    }
                    else{
                        echo "<hr>";
                    }
                    processXML($data);
                    }
                }  
                processXML($xml);

?>

Now i want to display full data of xml with the image so how can i display it.please help

Upvotes: 1

Views: 1572

Answers (1)

Krish R
Krish R

Reputation: 22711

Can you try this,

         $xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
            function processXML($node){
                foreach($node->children() as $agent => $data){      
                      $agent= trim($agent);   
                     if($agent!="" && $agent=='image'){
                           echo '<img src="'.$data.'" >';
                     }elseif($agent!=""){
                          echo $data;
                          echo "</br>";
                     }else{
                          echo "<hr>";
                     }    

                processXML($data);
                }
            }  
            processXML($xml);

Upvotes: 1

Related Questions