1

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

2
  • <img src="{$data->image}" />? Commented Jan 9, 2014 at 18:39
  • yes but where i have to place that code and how because you can see i displaying data through just echo $data; Commented Jan 9, 2014 at 18:40

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Correct but because of this image is displayed but due to echo $data; the link of image is also displayed so what can i do
No this is not possible because if we are check for image first and after that for whole data then data can not be print
Hey do you know how to skip first element from xml fine if yes then my problem can be solved
yes i had tried this code but as i am saying that if we are trying to check first for image and after that whole data then how it can be possible because the image is part of data not data is a part of image
@Sanjay Rathod, I have updated my answer can you please check?

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.