我无法访问SimpleXML对象中的@attribute
部分。当我使用var_dump
查看整个对象时,输出是正确的;当我var_dump
其他对象(嵌套标记)时,输出也是正确的。但是,当我按照文档中的方法使用var_dump
$xml->OFFICE->{'@attributes'}
时,输出为空对象,尽管第一个var_dump
明确显示有属性可输出。
请问有人知道我这里做错了什么吗?或者有什么办法可以解决这个问题吗?
我无法访问SimpleXML对象中的@attribute
部分。当我使用var_dump
查看整个对象时,输出是正确的;当我var_dump
其他对象(嵌套标记)时,输出也是正确的。但是,当我按照文档中的方法使用var_dump
$xml->OFFICE->{'@attributes'}
时,输出为空对象,尽管第一个var_dump
明确显示有属性可输出。
请问有人知道我这里做错了什么吗?或者有什么办法可以解决这个问题吗?
试试这个
$xml->attributes()->Token
attributes()
。例如,如果你有<root><tag attr="a">b</tag></root>
,那么你需要使用$xml->tag->attributes()->attr
来访问它。 - zysoft您可以在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";
}
$b
将会成为一个SimpleXMLElement
对象,所以print_r
和var_dump
将会输出奇怪的结果。你可以将其强制转换为字符串(或者其他任何你想要的类型)来避免这种情况发生。 - jxmallett我以前经常这样获取@attributes
,但是代码会更长一些。
$att = $xml->attributes();
echo $att['field'];
这样做会更加容易,你只需一次遵循以下格式即可获取属性:
$xml['field'];
其他替代品包括:
$xml->attributes()->{'field'};
$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];
$xml->attributes()->{'field'}
, $xml->attributes()->field
, 和 $f='field'; $xml->attributes()->$f;
是相同的。我看不出为什么其中一个是正确的格式,而另一个是错误的。 - zysoftstring
,以便获得 string
而不是 SimpleXMLElement
。(string) $xml->attributes()['attr']
- Z. Zlatev$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
。
事实上,get_properties
处理程序会严重误导 SimpleXMLElement。没有属性名为“@attributes”,因此您无法执行$sxml->elem->{"@attributes"}["attrib"]
。
get_properties
处理程序)运行。非常有趣。 - andrewtweber你可以直接这样做:
echo $xml['token'];
print_r($xml->xpath('@token'));
它帮助我将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
)
不幸的是,我使用的是独特的 PHP 5.5 版本(目前只能使用 Gentoo),我发现
$xml->tagName['attribute']
这是唯一有效的解决方案。我试过Bora提供的所有方法,包括“正确快速”格式,但它们都失败了。
虽然这是最简单的格式之一,但我不喜欢想着自己为什么要尝试其他人说可以的格式,感觉自己像疯了一样。
享受所值得的(我提到过独特的构建吗?)。
<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 ) ) ) ) ) )
问题已经解决:
<?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; }
?>