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等?