如何从PHP中的SimpleXMLObject解析值`@ attribute` [英] How to parse value `@attribute` from a SimpleXMLObject in PHP

查看:129
本文介绍了如何从PHP中的SimpleXMLObject解析值`@ attribute`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析starkoverflow.com/feeds/tag/{$tagName}. 这是我的代码:

I am trying to parse the starkoverflow.com/feeds/tag/{$tagName}. This is my code:

<?php
    $xml = file_get_contents("http://stackoverflow.com/feeds/tag/php");
    $simpleXml = simplexml_load_string($xml);
    $attr = $simpleXml->entry->category->@attributes;

?>

当我执行上面的代码时,它给我一个错误,Parse error: syntax error, unexpected '@', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in D:\wamp\www\success\protoT.php on line 4

When I execute the above code it gives me a error, Parse error: syntax error, unexpected '@', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in D:\wamp\www\success\protoT.php on line 4

所以,我的问题是如何获取@attributes的数组?

Scrrenshot

So, My question is how to get the array of the @attributes?

Scrrenshot

推荐答案

您使用适当记录的方法:attributes()

$attr = $simpleXml->entry->category->attributes();

$simpleXml->entry->category是一个数组,因此您需要指定要访问的数组条目:

Except that $simpleXml->entry->category is an array, so you need to specify which entry in the array you want to access:

$attr = $simpleXml->entry->category[0]->attributes();

编辑

除非我刚刚了解到,否则您只需要引用第一个元素

Unless, as I have just learned, you only need to reference the first element

这篇关于如何从PHP中的SimpleXMLObject解析值`@ attribute`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆