doupiai5597 2016-08-09 15:43
浏览 47

如何从标记名称开头有“@”的xml获取值?

I'm trying to get values from a XML file, but I have a problem. One of tag name starts with "@". So I'm getting an error on that.

This is my xml

object(SimpleXMLElement)#537 (1) { ["urun"]=> array(5225) { [0]=> object(SimpleXMLElement)#547 (1) { ["@attributes"]=> array(7) { ["id"]=> string(4) "2972" ["secenekid"]=> string(1) "4" ["grup"]=> string(4) "YAŞ" ["ozellik"]=> string(1) "1" ["fiyat"]=> string(1) "0" ["agirlik"]=> string(1) "0" ["Stok"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#548 (1) { ["@attributes"]=> array(7) { ["id"]=> string(4) "2972" ["secenekid"]=> string(1) "5" ["grup"]=> string(4) "YAŞ" ["ozellik"]=> string(1) "2" ["fiyat"]=> string(1) "0" ["agirlik"]=> string(1) "0" ["Stok"]=> string(1) "0" } } 

I'm trying to get like that.

$url = "http://xx.com";
$xml = simplexml_load_file($url);

foreach($xml->urun->@attributes as $val) {
echo $val->id; 
}

and this is the error what I see

syntax error, unexpected '@', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

so what should I do know for to solve that?

Thanks.

  • 写回答

2条回答 默认 最新

  • doufangxie0203 2016-08-09 15:48
    关注

    The curly braces syntax can be used to access such properties:

    $xml->urun->{'@attributes'}
    

    See PHP curly brace syntax for member variable

    In this case you could also use $xml->urun->attributes(), see Accessing @attribute from SimpleXML

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题