“在标志处” @ 在 SimpleXML 对象中?

发布于 2024-10-05 02:25:52 字数 189 浏览 4 评论 0原文

这是在典型的 SimpleXMLElement 对象上运行 print_r() 的输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (

        )
)

@ 符号是什么意思?

This is the output of print_r() run on a typical SimpleXMLElement object:

SimpleXMLElement Object
(
    [@attributes] => Array
        (

        )
)

What does the @ sign mean?

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

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

发布评论

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

评论(4

叹沉浮 2024-10-12 02:25:52

这是一个 SimpleXMLElement 对象。 “@attributes”行是 属性 的内部表示XML 元素。使用 SimpleXML 的函数从该对象获取数据,而不是直接与其交互。

This is a SimpleXMLElement object. The '@attributes' row is an internal representation of the attributes from the XML element. Use SimpleXML's functions to get data from this object rather than interacting with it directly.

七色彩虹 2024-10-12 02:25:52

所有关于错误控制的答案都是不正确的。 @ 并不意味着任何。这就是该属性在内部的调用方式,但不要依赖于此。处理 SimpleXML 时不要依赖 print_r()var_dump()。 SimpleXML 做了很多 print_r()var_dump() 无法正确表示的“神奇”事情。

如果您需要了解 XML 片段“内部”的内容,只需使用 ->asXML() 即可。

All those answers about error control are incorrect. The @ doesn't mean anything. That's how the property is called internally, but do not rely on this. Do not rely on print_r() or var_dump() when dealing with SimpleXML. SimpleXML does a lot of "magical" things that are not correctly represented by print_r() and var_dump().

If you need to know what's "inside" a XML fragment, just use ->asXML() on it.

九八野马 2024-10-12 02:25:52

抱歉,无法以访客身份发表评论,但对于像我一样最终来到这里的其他人...我正在创建自己的 Joomla 表单字段,而 Joomla 创建了一个非常“有趣”的各种事物对象。现在,我不想成为 SimpleXML 专家,我想要的只是隐藏在 @attributes 中的原始标签文本。

经过一段时间的“嗯,我想知道这是否有效?”™ 我发现这是访问这些值的最简单方法:

var_dump($simpleXMLObject);

/* Result */
object(SimpleXMLElement)
  public '@attributes' => 
    array (size=3)
      'name' => string 'awesome'
      'label' => string 'Awesome Label'
      'type' => string 'typeOfAwesome'

echo $simpleXMLObject->attributes()->label; // Awesome Label

$simpleXMLObject->attributes()->label = 'Different Day, Different Awesome';
echo $simpleXMLObject->attributes()->label; // Different Day, Different Awesome 

他们没有说谎。这确实很简单。

Sorry, can't comment as a guest but for anyone else who ends up here like I did... I am creating my own Joomla form fields and Joomla creates a very 'interesting' object of all sorts of things. Now, I didn't want to become a SimpleXML expert, all I wanted was the original label text which was squirrelled away in @attributes.

After a bit of "hmmm, I wonder if this works?"™ I found this is the easiest way of accessing these values:

var_dump($simpleXMLObject);

/* Result */
object(SimpleXMLElement)
  public '@attributes' => 
    array (size=3)
      'name' => string 'awesome'
      'label' => string 'Awesome Label'
      'type' => string 'typeOfAwesome'

echo $simpleXMLObject->attributes()->label; // Awesome Label

$simpleXMLObject->attributes()->label = 'Different Day, Different Awesome';
echo $simpleXMLObject->attributes()->label; // Different Day, Different Awesome 

They were not lying. It really is simple.

夏末染殇 2024-10-12 02:25:52

我正在使用仅提供 XML 格式数据的 HTTP API。所以首先我将它加载到 SimpleXML 中,并且还对 @attributes 问题感到困惑..我如何获取它包含的宝贵数据? print_r() 让我困惑。

我的解决方案是在 0 处创建一个数组和一个迭代器变量。使用 foreach 循环遍历 SimpleXML 对象,并使用 attribues() 方法获取数据并将其加载到我创建的数组。在 foreach 循环结束之前进行迭代。

所以 print_r() 从显示这个:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [ID] => 1
            [First] => John
            [Last] => Smith
        )
)

到一个更可用的普通数组。这很棒,因为我希望能够在需要时快速将数组转换为 json。

我的代码解决方案:

$obj = simplexml_load_string($apiXmlData);
$fugly = $obj->Deeply->Nested->XML->Data->Names;
$people = array();
$i = 0;
foreach($fugly as $val)
{
  $people[$i]['id'] += $val->attributes()->ID;
  $people[$i]['first'] = "". $val->attributes()->First;
  $people[$i]['last'] = "". $val->attributes()->Last;
  $i++;
}

快速说明 PHP 的 settype() 函数很奇怪/有缺陷,因此我添加了 + 以确保 ID 是整数,并添加了 + >引号以确保名称是字符串。如果没有某种类型的变量转换,您将把 SimpleXML 对象加载到您创建的数组中。

print_r()的最终结果:

Array
(
   [0] => Array
    (
        [id] => 1
        [first] => John
        [last] => Smith
    )

   [1] => Array
    (
        [id] => 2
        [first] => Jane
        [last] => Doe
    )
)

I am working with an HTTP API that gives out only XML formatted data. So first I loaded it into SimpleXML and was also puzzled by the @attributes issue.. how do I get at the precious data it contains? print_r() confused me.

My solution was to create an array and an iterator variable at 0. Loop through a SimpleXML object with foreach and get at the data with the attribues() method and load it into my created array. Iterate before foreach loop ends.

So print_r() went from showing this:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [ID] => 1
            [First] => John
            [Last] => Smith
        )
)

To a much more usable normal array. Which is great because I wanted the option to quickly convert array into json if needed.

My solution in code:

$obj = simplexml_load_string($apiXmlData);
$fugly = $obj->Deeply->Nested->XML->Data->Names;
$people = array();
$i = 0;
foreach($fugly as $val)
{
  $people[$i]['id'] += $val->attributes()->ID;
  $people[$i]['first'] = "". $val->attributes()->First;
  $people[$i]['last'] = "". $val->attributes()->Last;
  $i++;
}

Quick note would be PHP's settype() function is weird/buggy, so I added the + to make sure ID is an integer and added the quotes to make sure the name is string. If there isn't a variable conversion of some kind, you're going to be loading SimpleXML objects into the array you created.

Final result of print_r():

Array
(
   [0] => Array
    (
        [id] => 1
        [first] => John
        [last] => Smith
    )

   [1] => Array
    (
        [id] => 2
        [first] => Jane
        [last] => Doe
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文