views:

82

answers:

4

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.

A: 

Since you already have a DTD setup the esiest thing to do would be to add and IDENTITY attribute to the various tags and then use getElementById

IF you dont mind always dealing with nodelist collections you can always use getElementsByTagName.

Otherwise you have to use the traversal functions and element properties (nextSibling, prevSibling, childNodes) to traverse the DOM. You could also use xpath queries which would simplyfy the traversal.

prodigitalson
+2  A: 

Has your hosting provider disabled SimpleXML, or have thye just disabled the ability of SimpleXML to load files from URLs? The later is far more common than the former, and if it's the later DomDocument may suffer the same fate.

Before you dive too deep into DomDocument, which is a robust put poorly documented API, trying using the curl functions to download your XML, and then use simplexml_load_string to create your SimpleXML object.

If you end up having to go the DomDocument route, you want to search around for xpath tutorials. It's the most straightforward way to get information out of a DomDocument object.

Alan Storm
xpath, of course, is known for its straightforwardness.
Dereleased
Nope, simplexml is disavled with --disable-simplexml
tvanover
+2  A: 
<?php

$dom = new DOMDocument();
$dom->load( 'file.xml' );
$xp = new DOMXpath($dom);

$username = $xp->evaluate('//username');
$password = $xp->evaluate('//password');

if ( $username->length > 0 ) {
    echo $username->item(0)->nodeValue;
}

if ( $password->length > 0 ) {
    echo $password->item(0)->nodeValue;
}
meder
A: 

Or I could use the simple xml for php 4 class that behaves pretty much as simple xml, but works when simple-xml is disabled.

tvanover