DeTechn Blog

PHP解析XML

<?php
/**
 * 作为解析XML配置文件必备工具
 */
class XMLUtil {
    public static $dbconfigpath = "./db.config.xml";
    public static function getDBConfiguration() {
        $dbconfig = array ();
        try {
            // 读取配置文件内容
            $handle&nbsp;=&nbsp;fopen(self::$dbconfigpath, "r");
            $content&nbsp;=&nbsp;fread($handle, filesize(self::$dbconfigpath));
            // 获取xml文档根节点,进而获取相关的数据库信息
            $mysql&nbsp;=&nbsp;simplexml_load_string($content);

            // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用
            $dbconfig[&#39;host&#39;]&nbsp;=&nbsp;$mysql->host;
            $dbconfig[&#39;user&#39;]&nbsp;=&nbsp;$mysql->user;
            $dbconfig[&#39;password&#39;]&nbsp;=&nbsp;$mysql->password;
            $dbconfig[&#39;db&#39;]&nbsp;=&nbsp;$mysql->db;
            $dbconfig[&#39;port&#39;]&nbsp;=&nbsp;$mysql->port;
            // 将配置信息以关联数组的形式返回
            return $dbconfig;
        } catch ( Exception $e ) {
            throw new RuntimeException ( "<mark>读取数据库配置文件信息出错!</mark><br />" );
        }
        return $dbconfig;
    }
}
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »