I have a class that extends PDO to give it the ability to pull the configuration from an xml file. Unfortunately the our hosting provider has disabled SimpleXML so I need to refactor my code to use Dom, which is available.
I am using the following code:
class xmlPDO extends PDO
{
public function __construct($xml_uri){
$xml = simplexml_load_file($xml_uri);
$dsn_template = "%s:host=%s; dbname=%s";
$dsn = sprintf($dsn_template, $xml->dsn->driver, $xml->dsn->host, $xml->dsn->dbname);
parent::__construct($dsn, $xml->username, $xml->password);
}
}
To read the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database [
<!ELEMENT database (name, description, dsn, username, password)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT dsn (driver, dbname, host)>
<!ELEMENT driver (#PCDATA)>
<!ELEMENT dbname (#PCDATA)>
<!ELEMENT host (#PCDATA)>
<!ELEMENT username (#PCDATA)>
<!ELEMENT password (#PCDATA)>
]>
<database>
<name>Test Database</name>
<description>Localhost test database, use only for testing</description>
<dsn>
<driver>mysql</driver>
<dbname>test</dbname>
<host>localhost</host>
</dsn>
<username>user</username>
<password>test</password>
</database>
I am looking at the php.net documentation and am having a really hard time trying to figure out how I am supposed to be doing this. I can't seem to be any clear code examples of pulling out data like this.