扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:Jack D. Herrington 来源:IBM 2007年10月21日
关键字:
<?xml version="1.0"?> <config> <Title>My App</Title> <TemplateDirectory>tempdir</TemplateDirectory> </config> |
<?php class Configuration { private $configFile = 'config.xml'; private $items = array(); function __construct() { $this->parse(); } function __get($id) { return $this->items[ $id ]; } function parse() { $doc = new DOMDocument(); $doc->load( $this->configFile ); $cn = $doc->getElementsByTagName( "config" ); $nodes = $cn->item(0)->getElementsByTagName( "*" ); foreach( $nodes as $node ) $this->items[ $node->nodeName ] = $node->nodeValue; } } $c = new Configuration(); echo( $c->TemplateDirectory."\n" ); ?> |
... function save() { $doc = new DOMDocument(); $doc->formatOutput = true; $r = $doc->createElement( "config" ); $doc->appendChild( $r ); foreach( $this->items as $k => $v ) { $kn = $doc->createElement( $k ); $kn->appendChild( $doc->createTextNode( $v ) ); $r->appendChild( $kn ); } copy( $this->configFile, $this->configFile.'.bak' ); $doc->save( $this->configFile ); } ... |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。