This is a very simple but very useful function that seems like an obvious feature lacking from the PHP XML class. Typical. This should make adding multiple XML attributes per element a breeze and why an array isn’t what’s passed to xml::writeAttribute() already, or as an option, truly baffles my ass.

Here it is to save you a bunch of time:

function writeAttributeArray($xml,$attr_array)
    foreach($attr_array as $name=>$value )
        $xml->writeAttribute($name, $value);

While it doesn’t look too complicated, it isn’t. Very simple, pass in an $xml object created by XMLWriter and then create an element and assign attributes in bulk with one simple function.



This makes it exceptionally easy to add lots of attributes for XML elements used by some graphics packages, hint hint. Bet you can see where this is going.

When you run the above example it will create the following XML output:

<graph lineThickness="3" showValues="0" numdivlines="4" divLineColor="333333" numVDivLines="10" formatNumberScale="1" rotateNames="1" decimalPrecision="0" anchorRadius="2" anchorBgAlpha="0" numberPrefix="" divLineAlpha="30" showAlternateHGridColor="1" yAxisMinValue="800000" shadowAlpha="50">

Well that’s all for this blog post, we’ll pick up more XML fun later.

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>