135

我在访问@attributeSimpleXML 对象的部分时遇到问题。当我var_dump得到整个对象时,我得到正确的输出,当我得到var_dump对象的其余部分(嵌套标签)时,我得到正确的输出,但是当我按照文档和 时var_dump $xml->OFFICE->{'@attributes'},我得到一个空对象,尽管事实上第一个var_dump清楚地表明输出有属性。

任何人都知道我在这里做错了什么/我怎样才能做到这一点?

4

10 回答 10

147

试试这个

$xml->attributes()->Token
于 2012-09-26T10:57:47.720 回答
93

您可以通过在 XML 节点上调用 attributes() 函数来获取 XML 元素的属性。然后您可以 var_dump 函数的返回值。

更多信息在 php.net http://php.net/simplexmlelement.attributes

该页面的示例代码:

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
于 2009-10-30T20:37:35.433 回答
64

我以前用过很多次来得到@attributes像下面这样的东西,而且时间有点长。

$att = $xml->attributes();
echo $att['field'];

它应该更容易,您只能一次获得以下格式的属性:

标准方式 - 阵列访问属性 (AAA)

$xml['field'];

其他替代方案是:

正确和快速的格式

$xml->attributes()->{'field'};

错误的格式

$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];
于 2013-09-04T14:45:38.740 回答
43
$xml = <<<XML
<root>
<elem attrib="value" />
</root>
XML;

$sxml = simplexml_load_string($xml);
$attrs = $sxml->elem->attributes();
echo $attrs["attrib"]; //or just $sxml->elem["attrib"]

使用SimpleXMLElement::attributes.

事实是,SimpleXMLElementget_properties处理程序很重要。没有名为“@attributes”的属性,所以你不能这样做$sxml->elem->{"@attributes"}["attrib"]

于 2010-08-04T23:10:53.397 回答
15

你可以这样做:

echo $xml['token'];
于 2010-08-05T02:21:22.767 回答
8

如果您正在寻找这些属性的列表,XPath 将是您的朋友

print_r($xml->xpath('@token'));
于 2010-08-08T14:44:18.790 回答
4

它帮助我将 simplexml_load_file($file) 的结果转换为 JSON 结构并将其解码回来:

$xml = simplexml_load_file("$token.xml");
$json = json_encode($xml);
$xml_fixed = json_decode($json);

$try1 = $xml->structure->{"@attributes"}['value'];
print_r($try1);

>> result: SimpleXMLElement Object
(
)

$try2 = $xml_fixed->structure->{"@attributes"}['value'];
print_r($try2);

>> result: stdClass Object
(
    [key] => value
)
于 2016-02-12T12:53:28.217 回答
3

不幸的是,我有一个 PHP 5.5 的独特构建(暂时使用 Gentoo),而我发现的是

 $xml->tagName['attribute']

是唯一有效的解决方案。我尝试了上述 Bora 的所有方法,包括“Right & Quick”格式,但都失败了。

这是最简单的格式这一事实是一个加分项,但我不喜欢认为我在尝试其他人所说的所有格式都发疯了。

Njoy 的价值(我有没有提到独特的构建?)。

于 2015-01-08T14:47:01.567 回答
0

我想从外部 xml 文件中提取字符串(只是歌曲标题和艺术家姓名):https ://nostalgicfm.ro/NowOnAir.xml 这种 xml 形式:

 <Schedule System="Jazler">
     <Event status="happening" startTime="20:31:20" eventType="song">
        <Announcement Display=""/>
      <Song title="Let It Be ">
       <Artist name="Beatles">
        <Media runTime="265.186"/>
        <Expire Time="20:35:45"/>
       </Artist>
      </Song>
     </Event>
    </Schedule>

我尝试使用此代码 PHP,但我不知道如何提取名称和标题...例如“Beatles - Let It Be”

  <?php
    $url = "https://nostalgicfm.ro/NowOnAir.xml";
    $xml = simplexml_load_file($url);
    print_r($xml);
    ?>

结果是一个对象:

 SimpleXMLElement Object ( [@attributes] => Array ( [System] => Jazler ) [Event] => SimpleXMLElement Object ( [@attributes] => Array ( [status] => happening [startTime] => 20:51:21 [eventType] => song ) [Announcement] => SimpleXMLElement Object ( [@attributes] => Array ( [Display] => ) ) [Song] => SimpleXMLElement Object ( [@attributes] => Array ( [title] => If You Were A Sailboat ) [Artist] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Katie Melua ) [Media] => SimpleXMLElement Object ( [@attributes] => Array ( [runTime] => 228.732 ) ) [Expire] => SimpleXMLElement Object ( [@attributes] => Array ( [Time] => 20:55:09 ) ) ) ) ) ) 
于 2022-02-17T19:41:54.860 回答
0

自己解决了:

<?php 
$url = 'https://nostalgicfm.ro/NowOnAir.xml'; 
$xml = simplexml_load_file($url); 
foreach ( $xml->Event->Song->Artist->attributes() as $tag => $value ); 
foreach ( $xml->Event->Song->attributes() as $tag => $value1 ) { 
echo $value." - ".$value1.PHP_EOL; } 
?>
于 2022-02-23T09:17:37.743 回答