Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用xpath将节点值和属性作为数组获取_Php_Xml_Xpath - Fatal编程技术网

Php 使用xpath将节点值和属性作为数组获取

Php 使用xpath将节点值和属性作为数组获取,php,xml,xpath,Php,Xml,Xpath,是否有任何方法可以将所有属性及其值作为数组获取。 这里有一个节点 我需要的是得到一个像 $vehicle=array(“车轮”=>“四”,“颜色”=>“红色”)您可以使用simplexmlement解析来完成 $xml = '<vehicle wheels="four" color="red"/>'; $x = new SimpleXMLElement($xml); $array = current($x->attributes()); print_r($array);

是否有任何方法可以将所有属性及其值作为数组获取。 这里有一个节点

我需要的是得到一个像


$vehicle=array(“车轮”=>“四”,“颜色”=>“红色”)

您可以使用
simplexmlement
解析来完成

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

$x = new SimpleXMLElement($xml);
$array = current($x->attributes());
print_r($array);
$xml='';
$x=新的SimpleXMLElement($xml);
$array=当前($x->attributes());
打印(数组);

您可以使用
simplexmlement
解析来完成

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

$x = new SimpleXMLElement($xml);
$array = current($x->attributes());
print_r($array);
$xml='';
$x=新的SimpleXMLElement($xml);
$array=当前($x->attributes());
打印(数组);

我通过使用
属性
属性找到了解决方案

这是密码

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

$array将产生预期的结果…

我通过使用
属性
属性找到了解决方案

这是密码

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

$array将生成所需的…

您使用什么来解析XML、SimpleXML、DOMXPath/DOMDocument等?您使用什么来解析XML、SimpleXML、DOMXPath/DOMDocument等?