简体   繁体   English

PHP + XPath查询获取子节点及其值

[英]PHP + XPath Query get Child Nodes and their values

I have a XML file record.xml which looks like: 我有一个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>

I want to write a xpath query which will return all the child nodeName of <record> and value of all these child nodes in an associative array. 我想编写一个xpath查询,该查询将在关联数组中返回<record>所有子nodeName和所有这些子节点的值。

For Ex.: 例如:

For above XML file Output array should be- 对于上述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' } } 

Below is part of Code I written this return all the <record> child nodes values but not their node name. 以下是我编写的代码的一部分,该代码返回所有<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
     .....
     .....

I just want a Xpath query which will return child node names along with their values. 我只想要一个Xpath查询,该查询将返回子节点名称及其值。

Problem is a record as a node is just part of the node hierarchy. 问题是记录,因为节点只是节点层次结构的一部分。 What you're getting is all records, however you also want to descend and get the data from the record's child nodes. 您得到的是所有记录,但是您还想下降并从记录的子节点获取数据。 A very case specific example is : 一个非常具体的案例是:

<?php     
$xmldoc = new DOMDocument();
$xmldoc->load('record.xml');
$xpathvar = new Domxpath($xmldoc);
$res = $xpathvar->query('//record');
foreach($res as $data){
    $narr = [];
    foreach ($data->childNodes as $cnode) {
        $narr[$cnode->nodeName] = $cnode->nodeValue;
    }
    $arr[] = $narr;

}    
print_r($arr);

Should output something like: 应该输出类似:

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
        )

)

NOTE: This solution is very case specific and would probably break if you have additional nested hierarchies (which I recommend you do in order to store the multiple subjects). 注意:此解决方案是针对特定情况的,如果您有其他嵌套层次结构(建议您这样做以存储多个主题),则该解决方案可能会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM