我在访问@attribute
SimpleXML 对象的部分时遇到问题。当我var_dump
得到整个对象时,我得到正确的输出,当我得到var_dump
对象的其余部分(嵌套标签)时,我得到正确的输出,但是当我按照文档和 时var_dump
$xml->OFFICE->{'@attributes'}
,我得到一个空对象,尽管事实上第一个var_dump
清楚地表明输出有属性。
任何人都知道我在这里做错了什么/我怎样才能做到这一点?
试试这个
$xml->attributes()->Token
您可以通过在 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";
}
我以前用过很多次来得到@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 = <<<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"]
。
你可以这样做:
echo $xml['token'];
如果您正在寻找这些属性的列表,XPath 将是您的朋友
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 的所有方法,包括“Right & Quick”格式,但都失败了。
这是最简单的格式这一事实是一个加分项,但我不喜欢认为我在尝试其他人所说的所有格式都发疯了。
Njoy 的价值(我有没有提到独特的构建?)。
我想从外部 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 ) ) ) ) ) )
自己解决了:
<?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; }
?>