我有一个 XML 文件 record.xml,它看起来像:
<record>
<name>john</name>
<gender>male</gender>
<subject>mathematics, english, science</subject>
</record>
<record>
<name>jamie</name>
<gender>female</gender>
<subject>mathematics, science</subject>
</record>
<record>
<name>jack</name>
<gender>male</gender>
<subject>social-science, english</subject>
</record>
我想编写一个 xpath 查询,它将返回<record>
关联数组中所有这些子节点的所有子节点名和值。
例如:
对于上述 XML 文件输出数组应该是 -
array{ [0] => array{ [name] = 'john', [gender] = 'male', [subject] = 'mathematics, english, science' } [1] => array{ [name] = 'jamie', [gender] = 'female', [subject] = 'mathematics, science' } [2] => array{ [name] = 'jack', [gender] = 'male', [subject] = 'social-science, english' } }
下面是我编写的代码的一部分,它返回所有<record>
子节点值,但不返回它们的节点名称。
.......
.......
$xmldoc = new DOMDocument();
$xmldoc->load('record.xml');
$xpathvar = new Domxpath($xmldoc);
$res = $xpathvar->query('//record');
foreach($res as $data){
//$data do not contain node values
$arr[] = $data->textContent;
}
//process $arr to get required format
.....
.....
我只想要一个 Xpath 查询,它将返回子节点名称及其值。