使用 xPath 访问 simpleXML 的值 [英] Using xPath to access values of simpleXML

查看:31
本文介绍了使用 xPath 访问 simpleXML 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个包含设置的 XML 对象结果.

I have a XML object result from my database containing settings.

我正在尝试访问特定设置名称的值:

I am trying to access the values for a particular settingName:

    SimpleXMLElement Object
(
[settings] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [settingName] => Test
                [settingDescription] => Testing
                [requireValue] => 1
                [localeID] => 14
                [status] => 1
                [value] => 66
                [settingID] => 5
            )

        [1] => SimpleXMLElement Object
            (
                [settingName] => Home Page Stats
                [settingDescription] => Show the Top 5 Teammate / Teamleader stats?
                [requireValue] => 0
                [localeID] => 14
                [status] => 0
                [value] => SimpleXMLElement Object
                    (
                    )

                [settingID] => 3
            )

    )

)

我尝试使用 xPath 并且到目前为止有这个:

I tried using xPath and have this so far:

$value = $fetchSettings->xpath("//settingName[text()='Test']/../value");

返回:

Array ( [0] => SimpleXMLElement Object ( [0] => 66 ) )

如何获取实际值而不仅仅是另一个数组/对象?

How can I get the actual value and not just another array/object?

对于上面的示例,最终结果将是 66.

The end result will just be 66 for the example above.

推荐答案

  1. SimpleXMLElement::xpath() 返回搜索结果"的普通 PHP 数组;第一个结果将始终是索引 0 如果找到任何结果.
  2. 每个搜索结果"都是一个 SimpleXMLElement 对象,它有一个神奇的 __toString() 获取节点直接文本内容的方法(包括 CDATA,但包括子节点内的文本等).调用它的最简单方法是使用 (string)$my_element;(int)$my_element 也会调用它,然后将结果转换为整数.
  1. SimpleXMLElement::xpath() returns a plain PHP array of "search results"; the first result will always be index 0 if any results were found.
  2. Each "search result" is a SimpleXMLElement object, which has a magic __toString() method for getting the direct text content of a node (including CDATA, but including text inside child nodes, etc). The simplest way to call it is with (string)$my_element; (int)$my_element will also invoke it, then convert the result to an integer.

所以:

 $xpath_results = $fetchSettings->xpath("//settingName[text()='Test']/../value");
 if ( count($xpath_results) > 0 ) {
      $value = (string)$xpath_results[0];
 }

<小时>

或者,DOMXPath可以返回元素和属性节点以外的结果,由于 DOM 更丰富的对象模型.例如,您可以使用一个以 //text() 结尾的 XPath 表达式来引用节点的文本内容,而不是节点本身(SimpleXML 将执行搜索,但会给您一个元素对象无论如何).


Alternatively, the DOMXPath class can return results other than element and attribute nodes, due to the DOM's richer object model. For instance, you can have an XPath expression ending //text() to refer to the text content of a node, rather than the node itself (SimpleXML will do the search, but give you an element object anyway).

缺点是使用起来比较冗长,但幸运的是您可以混合和匹配两组函数(使用 dom_import_simplexml() 及其对应物),因为它们具有相同的底层表示:

The downside is it's rather more verbose to use, but luckily you can mix and match the two sets of functions (using dom_import_simplexml() and its counterpart) as they have the same underlying representation:

 // WARNING: Untested code. Please comment or edit if you find a bug!
 $fetchSettings_dom = dom_import_simplexml($fetchSettings);
 $xpath = new DOMXPath($fetchSettings_dom->ownerDocument);
 $value = $xpath->evaluate(
      "//settingName[text()='Test']/../value/text()",
      $fetchSettings_dom
 );

这篇关于使用 xPath 访问 simpleXML 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆