如何通过php从xml显示图像?

时间:2014-01-09 18:35:47

标签: php xml

我有两个文件note.xml和xml.php

这是我的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>

这是我的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);

?>

现在我想用图像显示xml的完整数据,以便我如何显示它。请帮助

1 个答案:

答案 0 :(得分:1)

你能试试吗,

         $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);