php之XMLReader简单事例

新建xml.xml

<?xml version="1.0" encoding="utf-8"?>  
<shows>  
    <show>  
        <name>action</name>
        <age>18</age>
        <sex>男</sex>
    </show>  
    <show>  
        <name>yiyi</name>
        <age>20</age>
        <sex>女</sex>
    </show>  
</shows>  
xml.php如下
<?php
    $items = [];
    $reader = new XMLReader();
    $reader->open('xml.xml','utf-8');

    while($reader->read()){
        if($reader->name == 'show' && $reader->nodeType == XMLReader::ELEMENT){
            $item = [];
            while($reader->read() && $reader->name != 'show'){
                if($reader->nodeType != XMLReader::ELEMENT)continue;

                $name = $reader->name;
                $value = $reader->readString();
                $item[$name]  = $value;
            }
            $items[] = $item;

        }

    }
    echo "<pre>";
    print_r($items);


分类:   程序积累  /   /  浏览量:  267
2018-9-5 16:09 Wednesday