Using xPath to access values of simpleXML

若如初见. 提交于 2019-12-11 05:55:31

问题


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
            )

    )

)

I tried using xPath and have this so far:

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

which returns:

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

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

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


回答1:


  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.

So:

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

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

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



回答2:


Because every element in a XML-file can appear as multiple times the parser always returns an array. If you are sure, that it is only a single item you can use current()

echo (string) current($value);

Note, that I cast the SimpleXMLElement to a string (see http://php.net/manual/simplexmlelement.tostring.php ) to get the actual value.




回答3:


Use DomXPath class instead. http://php.net/manual/en/domxpath.evaluate.php The sample from php.net is just equivalent what you'd like to achieve:

<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
$tbody = $doc->getElementsByTagName('tbody')->item(0);
// our query is relative to the tbody node
$query = 'count(row/entry[. = "en"])';
$entries = $xpath->evaluate($query, $tbody);
echo "There are $entries english books\n";

In this way, you can get values straight from the XML.



来源:https://stackoverflow.com/questions/25025120/using-xpath-to-access-values-of-simplexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!