获取对某个XML区域的访问权限

时间:2012-10-30 20:36:56

标签: php xml simplexml

我在下面有以下XML结构,但我不确定如何直接访问该网址,以便我可以在我的图片代码中将其用作src

PHP结构:

$images = $url->property->photos
<img src="'.$images.'" width="233" height="154" /> - Testing URL  

常规XML结构:

<photos>
 <photo>
  Information
 </photo>
</photos>

XML结构:

["photos"]=>
    object(SimpleXMLElement)#118 (2) {
      ["@attributes"]=>
      array(1) {
        ["count"]=>
        string(2) "43"
      }
      ["photo"]=>
      array(43) {
        [0]=>
        string(97) "http://www.domain.co.nz/images/6f8bc4855bface1ad2c35635625fdbb4c3a11537.jpeg"
        [1]=>
        string(97) "http://www.domain.co.nz/images/fcb36e4a3465b934490d44e6bb8e1a8192cb466d.jpeg"
        [2]=>
        string(97) "http://www.domain.co.nz/images//c1e2c792832a08f881bb24756137a63a19c7070d.jpeg"
        [3]=>
        string(97) "http://www.domain.co.nz/images/b6d9a3c52c7fba81553edb9101c80d273c16ee3c.jpeg"

2 个答案:

答案 0 :(得分:1)

$xmlstr = <<<XML
<properties count="1">
        <property approved="yesterday">
                <address>123 Main Street</address>
                <photos count="2">
                        <photo>1.jpg</photo>
                        <photo>2.jpg</photo>
                </photos>
        </property>
</properties>
XML;
$properties = new SimpleXMLElement($xmlstr);//this could have been done using simplexml_load_file($url); for instance
//the important part is that you have a SimpleXML object named $properties
foreach($properties->property as $property){
        echo $property['approved']."<br>\n";
        echo $property->address."<br>\n";
        foreach($property->photos->photo as $photo){
                echo $photo."<br>\n";
        }
}

我假设您可能有多个属性,并且您希望列出每个属性。如果您只有一个,或者只想列出第一个,则可以安全地删除第一个foreach循环:

echo $properties->property[0]['approved']."<br>\n";
echo $properties->property[0]->address."<br>\n";
foreach($properties->property[0]->photos->photo as $photo){
    echo $photo."<br>\n";
}

答案 1 :(得分:0)

如果你像这样var_dump:

var_dump($images);
//output:
object(SimpleXMLElement)#117 (2) {...

您可以像这样循环访问这些照片网址:

foreach($images->photo as $image){
    echo '<img src="'.$image.'"/>;
}