当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Get node values and attributes as an array using xpath

问题描述:

Is there any way to get all attributes and its values as an array.

Here I have a node

<vehicle wheels="four" color="red"/>

what i need is to get an array like

$vehicle = array("wheels" => "four", "color" => "red");

网友答案:

You can do it using SimpleXMLElement parsing.

$xml = '<vehicle wheels="four" color="red"/>';

$x = new SimpleXMLElement($xml);
$array = current($x->attributes());
print_r($array);
网友答案:

I found the solution by using attributes property.

Here is the code

foreach ($vehicle->attributes as $attribName => $attribute_node)
{
    $array[$attribName] = $attribute_node->nodeValue;
}

The $array will produce what is expecting...

分享给朋友:
您可能感兴趣的文章:
随机阅读: