doq70020 2011-08-05 20:53
浏览 73

使用attribute定位xml节点,然后使用simpleXML返回不同属性的值

I have some xml:

<release id="2276808" status="Accepted">
    <images>
         <image height="600" type="primary" uri="http://s.dsimg.com/image/R-2276808-1302966902.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966902.jpeg" width="600"/>                       
         <image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966912.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966912.jpeg" width="600"/>  
         <image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966919.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966919.jpeg" width="600"/><image height="600" type="secondary" uri="http://s.dsimg.com/image/R-2276808-1302966929.jpeg" uri150="http://s.dsimg.com/image/R-150-2276808-1302966929.jpeg" width="600"/>
    </images> ...

I'm using SimpleXML and php 5.3.

I want to target the image node where type="primary" and return the value for the uri attribute.

The closest I've gotten is:

$xml->xpath('/release/images/image[@type="primary"]')->attributes()->uri;

which fails because you cannot call attribute() method after xpath.

  • 写回答

4条回答 默认 最新

  • dtnat7146 2011-08-08 18:29
    关注

    How about this:

    $xml = new SimpleXMLElement(file_get_contents('feed.xml'));
    $theUriArray = $xml->xpath('/release/images/image[@type="primary"]');
    $theUri = $theUriArray[0]->attributes()->uri;
    
    echo($theUri);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?