Glen Solsberry
Glen Solsberry

Reputation: 12320

Php wrapper class for XML

I'm working on a new class to wrap XML handling. I want my class to use simplexml if it's installed, and the built in XML functions if it's not. Can anyone give me some suggestions on a skeleton class to do this? It seems "wrong" to litter each method with a bunch of if statements, and that also seems like it would make it nearly impossible to correctly test.

Any upfront suggestions would be great!

EDIT: I'm talking about these built-in xml functions.

Upvotes: 0

Views: 1239

Answers (3)

null
null

Reputation: 7594

I've made a class which wraps SimpleXml functionality... take what you may from it...

bXml.class.inc

There is one weird thing... it's that SimpleXml doesn't allow its constructor to be overloaded, so you can't do things at initiation ... like override the input value (i.e. so you can accept XML as in input). I got around that limitation by using an ArrayObject class to wrap the new SimpleXml class.

Upvotes: 1

danp
danp

Reputation: 15251

I use something like this for doing xml translations and content:

Assuming xml structure something like this (important to use a regular structure, means you can pull off some nice agile tricks!):

<word name="nameofitem">
    <en>value</en>
    <pt>valor</pt>
    <de>value_de</de>
</word>

and then a class to handle the xml:

class translations
{

    public $xml = null;

    private $file = null;
    private $dom = null;



    function __construct($file="translations")  {
        // get xml

        $this->file = $file;

        $this->haschanges = false;

        $this->xml = file_get_contents($_SERVER['DOCUMENT_ROOT']."/xml/".$file.".xml");
        $this->dom = new DOMdocument();

        $this->dom->loadXML($this->xml);
    }

    function updateNode($toupdate, $newvalue, $lang="pt",$rootnode="word"){

        $this->haschanges = true;

        $nodes = $this->dom->getElementsByTagName($rootnode);
        foreach ($nodes as $key => $value) {
            if ($value->getAttribute("name")==$toupdate) {

                $nodes->item($key)->getElementsByTagName($lang)->item(0)->nodeValue = htmlspecialchars($newvalue,ENT_QUOTES,'UTF-8');
            }
        }
    }


    function saveUpdated(){
        $toSave =  $this->dom->saveXML();
        if ($this->haschanges === true) {
            file_put_contents($_SERVER['DOCUMENT_ROOT']."/xml/".$this->file.".xml", $toSave);
            return true;
        }
        else {
            return false;
        }
    }

}

I took out a few of the methods I have, for brevity, but I extend this with things to handle file and image uploads etc too.

Once you have all this you can do:

$xml = new translations();
    // loop through all the language posts
    foreach ($_POST["xml"]["en"] as $key => $value) {
        $xml->updateNode($key, stripslashes($value), "en");
    }

Or something ;) hope this gives you some ideas!

Upvotes: 0

troelskn
troelskn

Reputation: 117567

Which built-in xml functions are you referring to? SimpleXml is a standard extension, which uses libxml underneath - just as the dom extension does. So if the dom extension is installed, chances are that so is SimpleXml.

Upvotes: 1

Related Questions