php 从 SimpleXML 访问 @attribute

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1652128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:24:17  来源:igfitidea点击:

Accessing @attribute from SimpleXML

phpxmlsimplexml

提问by benjy

I am having a problem accessing the @attributesection of my SimpleXML object. When I var_dumpthe entire object, I get the correct output, and when I var_dumpthe rest of the object (the nested tags), I get the correct output, but when I follow the docs and var_dump$xml->OFFICE->{'@attributes'}, I get an empty object, despite the fact that the first var_dumpclearly shows that there are attributes to output.

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

Anyone know what I am doing wrong here/how I can make this work?

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

采纳答案by Niels Bom

You can get the attributes of an XML element by calling the attributes() function on an XML node. You can then var_dump the return value of the function.

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

More info at php.net http://php.net/simplexmlelement.attributes

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

Example code from that page:

该页面的示例代码:

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

回答by zysoft

Try this

尝试这个

$xml->attributes()->Token

回答by Bora

I used before so many times for getting @attributeslike below and it was a little bit longer.

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

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

It should be more easy and you can get attributes following format only at once:

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

Standard Way - Array-Access Attributes (AAA)

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

$xml['field'];

Other alternatives are:

其他替代方案是:

Right & Quick Format

正确且快速的格式

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

Wrong Formats

错误的格式

$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];

回答by Artefacto

$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"]

Use SimpleXMLElement::attributes.

使用SimpleXMLElement::attributes.

Truth is, the SimpleXMLElement get_propertieshandler lies big time. There's no property named "@attributes", so you can't do $sxml->elem->{"@attributes"}["attrib"].

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

回答by Alix Axel

You can just do:

你可以这样做:

echo $xml['token'];

回答by Cory Collier

If you're looking for a list of these attributes though, XPath will be your friend

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

print_r($xml->xpath('@token'));

回答by t2m

It helped me to convert the result of simplexml_load_file($file) into a JSON Structure and decode it back:

它帮助我将 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
)

回答by Gerard ONeill

Unfortunately I have a unique build (stuck with Gentoo for the moment) of PHP 5.5, and what I found was that

不幸的是,我有一个独特的 PHP 5.5 构建(目前坚持使用 Gentoo),我发现的是

 $xml->tagName['attribute']

was the only solution that worked. I tried all of Bora's methods above, including the 'Right & Quick' format, and they all failed.

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

The fact that this is the easiest format is a plus, but didn't enjoy thinking I was insane trying all of the formats others were saying worked.

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

Njoy for what its worth (did I mention unique build?).

Njoy 的价值(我提到过独特的构建吗?)。