如何使用SimpleXML(PHP)读取自关闭标记的属性


How can I read attributes of a self closing tag using SimpleXML (PHP)

我有一个XML文件,其中包含一些自关闭标记。

<tagname attr1="foo1" attr2="foo2" />

我使用PHP的simplexml将XML文件加载到一个对象中。到目前为止还不错。

问题出在自闭标签上。(print_r返回一个空对象)

我无法访问它们的属性。

===更新======

它是这样工作的:

$xml = simplexml_load_string($string);
echo $xml->tagname['attr1'];

您可以使用PHP库中与SimpleXML相关的SimpleXMLElement

http://www.php.net/manual/en/simplexmlelement.attributes.php

public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )