dongtan9518 2017-03-10 06:33
浏览 32
已采纳

区分PHP XPath中的相同节点名称

I am using PHP DOM XPath to read XML. XML is having following structure where I got stuck

<details>
    <name>name1</name>
    <address>address1</address>
</details>
<details>
    <name>name2</name>
    <mobile>mobileNum</mobile>
    <address>address2</address>
</details>

I have array like

  array(
        'name',
        'mobile',
        'address'
    ); 

I use this array and read values from XML and store that values in other array but in the first record mobile number is not present so it read mobile no from second record and inserts it in first record.

Expected output is

array
(

    [0] => array(

                'person_name' = name1,
                'address' = address1
                )
    [1] => array(

                'person_name' = name1,
                'mobile_no' = mobileNum
                'address' = address1
                )
)

But I get output as

array
(

    [0] => array(

                'person_name' = name1,
                'mobile_no' = mobileNum
                'address' = address1
                )
    [1] => array(

                'person_name' = name1,
                'address' = address1
                )
)

How can I differentiate values between two nodes with same name.

Code to read XML is

$nodes = array
(
    'person_name' => 'name',
    'mobile_no'   => 'mobile',
    'address'     => 'address'
)

$final_data = array();

$node_values = '';

foreach($nodes as $key => $data)
{   
    $node_values = $xml->xpath('//details'.$data); 

    $node_values = json_decode(json_encode((array)$node_values), TRUE);

    if(!empty($node_values))    
    {   
        $i = 0;

        foreach($node_values as $d)
        {
            $final_data[$i][$key] = trim($d[0]);

            $i++;
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongmu4591 2017-03-10 11:40
    关注

    Here's a working example with comments explaining how it works:

    https://3v4l.org/ICO5i

    <?php
    
    // First of all, I've added <root> element to your XML document,
    // because otherwise it's invalid. 
    // But it's not important for the rest of the code.
    //
    // Also I've added additional <somethingelse> tag to show that filtering is working
    $xmlString = '<root><details>
        <name>name1</name>
        <address>address1</address>
    </details>
    <details>
        <name>name2</name>
        <mobile>mobileNum</mobile>
        <address>address2</address>
        <somethingelse>This will be filtered</somethingelse>
    </details></root>';
    
    
    $xml = new SimpleXMLElement($xmlString);
    //array_flip to get node names as keys for later foreach loop
    $nodes = array_flip(array
    (
        'person_name' => 'name',
        'mobile_no'   => 'mobile',
        'address'     => 'address',
    ));
    
    $final_data = array();
    
    //Here are all <details> sections' data in array.
    $node_values = $xml->xpath('//details'); 
    $node_values = json_decode(json_encode((array)$node_values), TRUE);
    
    //this loop filters XML data from keys not existing in $nodes,
    // which are the only that you want to keep
    foreach($node_values as $node) {
        $final_data[] = array_intersect_key($node, $nodes);
    }
    
    var_dump($final_data);
    

    I assumed, that you want to filter XML data that it's not listed in $nodes array. Otherwise, if you don't mind having more data than you need, you could just skip the foreach loop and use $node_values as your final data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,