使用php搜索特定值的xml

时间:2017-06-14 07:30:36

标签: php json xml

http://exapmle.com/pub/static/version1497424536/frontend/Magento/luma/en_US/css/styles-m.css

       我想在xml文件中搜索特定值,例如    v =“9617.5”,v =“ - 14”,v =“ - 0.145356”。我设法打印获取数组。但是无法从数组中获取值。

2 个答案:

答案 0 :(得分:0)

使用Xpath:

$xrf = new SimpleXMLElement($xml);
foreach ($xrf->xpath('//*[@v="9617.5"]') as $x) {
  var_dump($x);
}

Xpath是一种表达式语言,用于获取DOM的一部分(SimpleXML是DOM之上的抽象)。它功能非常强大,但如果您将其与SimpleXMLElement::xpath()一起使用,则会受到一些限制。要获得完全支持,您必须使用DOMXpath::evaluate()

答案 1 :(得分:-1)

试试这个。

// Loading the XML into a SimpleXmlElement object.
$xml = simplexml_load_string($xml);

echo '<pre>';
$list = recursiveNodeIteration($xml);
print_r($list);
// Reads the node attributes from the given node.
function getAttributesFromNode(SimpleXMLElement $node)
{
    $nodeAttributes = [];
    foreach ($node->attributes() as $idx => $attribute)
    {
        $nodeAttributes[$idx] = (string)$attribute;
    }
    return $nodeAttributes;
}

// A function to recursively run through the node and its children.
function recursiveNodeIteration(SimpleXMLElement $node)
{
    // Get the node name for reference.
    $nodeName   = $node->getName();
    // Retrieve the attributes
    $attributes = getAttributesFromNode($node);
    $children   = [];
    // Check to see if this node has any children.
    if ($node->count() > 0)
    {
        // If so, run through each child element and retrieve the data.
        foreach ($node->children() as $child)
        {
            $children[] = recursiveNodeIteration($child);
        }
    }
    // Wrap it up in an array and return the dataset.

    return [
        'name'       => $nodeName,
        'attributes' => $attributes,
        'children'   => $children
    ];
}

这是你的结果:

Array
(
    [name] => XRF
    [attributes] => Array
        (
            [r] => 9.0.2
            [c] => iD2
            [g] => 
            [u] => SG31378-narnapid01
            [k] => 969232067
            [d] => 20170613
            [t] => 111536
        )

    [children] => Array
        (
            [0] => Array
                (
                    [name] => IL
                    [attributes] => Array
                        (
                            [k] => 1
                        )

                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => I
                                    [attributes] => Array
                                        (
                                            [k] => 86103141,812,333
                                        )

                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => M
                                                    [attributes] => Array
                                                        (
                                                            [k] => 6
                                                            [b] => 62409
                                                            [c] => 333
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [name] => M
                                                    [attributes] => Array
                                                        (
                                                            [k] => 186
                                                            [b] => 62409
                                                            [v] => 543014
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [name] => P
                                                    [attributes] => Array
                                                        (
                                                            [k] => 3,1,1
                                                            [s] => 1
                                                            [td] => 10
                                                            [v] => 9617.5
                                                            [t] => 190350
                                                            [d] => 20170613
                                                            [z] => 1
                                                            [n] => 273
                                                            [b] => 635
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [3] => Array
                                                (
                                                    [name] => P
                                                    [attributes] => Array
                                                        (
                                                            [k] => 33,537,1
                                                            [s] => 1
                                                            [td] => 10
                                                            [v] => -14
                                                            [t] => 190350
                                                            [d] => 20170613
                                                            [z] => 1
                                                            [n] => 273
                                                            [b] => 635
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [4] => Array
                                                (
                                                    [name] => P
                                                    [attributes] => Array
                                                        (
                                                            [k] => 33,579,1
                                                            [s] => 1
                                                            [td] => 10
                                                            [v] => -0.145356
                                                            [t] => 190350
                                                            [d] => 20170613
                                                            [n] => 273
                                                            [b] => 635
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                            [5] => Array
                                                (
                                                    [name] => P
                                                    [attributes] => Array
                                                        (
                                                            [k] => 12,0,1
                                                            [vt] => 1
                                                            [s] => 1
                                                            [td] => 10
                                                            [v] => 9617.5
                                                            [t] => 190350
                                                            [d] => 20170613
                                                            [z] => 1
                                                            [n] => 273
                                                            [b] => 635
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)