php xml 文件读取 XMLReader

Posted ~~逍遥~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php xml 文件读取 XMLReader相关的知识,希望对你有一定的参考价值。

php xml 文件读取

<?php
/**
$xmlString = ‘<xml>
        <persons count="10">
            <person username="username1" age="20">this is username1 description</person>
            <person username="username2" age="20">this is username2 description</person>
        </persons>
    </xml>‘;
    
$mXmlTest = new XmlTest();
$dataList = $mXmlTest->parseXmlStr($xmlString);
print_r($dataList);

$xmlFile = ‘./test.xml‘;
$dataList = $mXmlTest->parseXmlFile($xmlFile);
print_r($dataList);

 */


class XmlTest{
        
//     <xml>
//         <persons count="10">
//             <person username="username1" age="20">this is username1 description</person>
//             <person username="username2" age="20">this is username2 description</person>
//         </persons>
//     </xml>
    
    /**
     * 解析返回的XML文档
     */
    public function parseXmlFile($xmlFile){
        $reader = new \XMLReader();
        $reader->open($xmlFile, ‘UTF-8‘);
        $nodeName = ‘‘;
        $dataList = array();
        $data = array();
        while ($reader->read()){
            if($reader->nodeType == \XMLReader::ELEMENT){
                $nodeName = $reader->name;
                if($nodeName==‘persons‘){
                    $count = $reader->getAttribute(‘count‘);
                    if(!($count>0)){
                        break;
                    }
                }
                elseif($nodeName==‘person‘){
                    $data = array(
                        ‘username‘=>$reader->getAttribute(‘username‘),
                        ‘age‘=>$reader->getAttribute(‘age‘),
                    );
                }
            }
            if($reader->nodeType == \XMLReader::TEXT && !empty($nodeName)){
                if($nodeName==‘person‘){
                    $data[‘description‘] = strtolower($reader->value);
                    $dataList[] = $data;
                }
            }
        }
        $reader->close();
        return $dataList;
    }
    
    /**
     * 解析返回的XML文档
     */
    public function parseXmlStr($xmlString){
        $reader = new \XMLReader();
        $reader->xml($xmlString,‘UTF-8‘);
        $nodeName = ‘‘;
        $dataList = array();
        $data = array();
        while ($reader->read()){
            if($reader->nodeType == \XMLReader::ELEMENT){
                $nodeName = $reader->name;
                if($nodeName==‘persons‘){
                    $count = $reader->getAttribute(‘count‘);
                    if(!($count>0)){
                        break;
                    }
                }
                elseif($nodeName==‘person‘){
                    $data = array(
                        ‘username‘=>$reader->getAttribute(‘username‘),
                        ‘age‘=>$reader->getAttribute(‘age‘),
                    );
                }
            }
            if($reader->nodeType == \XMLReader::TEXT && !empty($nodeName)){
                if($nodeName==‘person‘){
                    $data[‘description‘] = strtolower($reader->value);
                    $dataList[] = $data;
                }
            }
        }
        return $dataList;
    }
}

 

以上是关于php xml 文件读取 XMLReader的主要内容,如果未能解决你的问题,请参考以下文章

PHP打开gzip压缩的XML

使用 XmlReader 读取属性值

使用 XMLreader 读取和解析大型 XML 文件。空值问题

XmlReader.ReadToFollowing方法

PHP XMLReader 解析行 2 次

使用 PHP 和 XMLReader 解析 XML