从 SimpleXML 访问@attribute

发布于 2024-09-13 09:59:35 字数 314 浏览 9 评论 0原文

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

有人知道我在这里做错了什么/我如何才能完成这项工作?

I am having a problem accessing the @attribute section of my SimpleXML object. When I var_dump the entire object, I get the correct output, and when I var_dump the 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_dump clearly shows that there are attributes to output.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

海未深 2024-09-20 09:59:35

试试这个

$xml->attributes()->Token

Try this

$xml->attributes()->Token
み零 2024-09-20 09:59:35

您可以通过调用 XML 节点上的 attribute() 函数来获取 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";
}

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.

More info at 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";
}
嘴硬脾气大 2024-09-20 09:59:35

我之前使用过很多次来获取 @attributes ,如下所示,它有点长。

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

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

标准方式 - 数组访问属性(AAA)

$xml['field'];

其他替代方法是:

Right&快速格式化

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

错误格式

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

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

$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)

$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'];
送舟行 2024-09-20 09:59:35
$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

事实是,SimpleXMLElement get_properties 处理程序谎言很大。没有名为“@attributes”的属性,因此您无法执行 $sxml->elem->{"@attributes"}["attrib"]

$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.

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

明月夜 2024-09-20 09:59:35

你可以这样做:

echo $xml['token'];

You can just do:

echo $xml['token'];
攒眉千度 2024-09-20 09:59:35

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

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

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

print_r($xml->xpath('@token'));
心碎无痕… 2024-09-20 09:59:35

不幸的是,我有一个独特的 PHP 5.5 版本(暂时使用 Gentoo),我发现这

 $xml->tagName['attribute']

是唯一有效的解决方案。我尝试了上面所有 Bora 的方法,包括“右”和“右”。快速格式化,但都失败了。

事实上,这是最简单的格式是一个优点,但我不喜欢认为我疯狂地尝试其他人所说的所有格式。

很喜欢它的价值(我有没有提到独特的构建?)。

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

 $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.

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?).

优雅的叶子 2024-09-20 09:59:35

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

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

$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
)
柳若烟 2024-09-20 09:59:35

我想从外部 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 ) ) ) ) ) ) 

I want to extract string (just Song title and Artist name) from external xml file: https://nostalgicfm.ro/NowOnAir.xml
This form of 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>

I try this code PHP but i don't know how to extract name & title...like "Beatles - Let It Be"

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

Result is an Oject:

 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 ) ) ) ) ) ) 
倾其所爱 2024-09-20 09:59:35

我自己解决了:

<?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; } 
?>

Resolved it myself:

<?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; } 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文