Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: five plus three?
(Example: nine)

The Note You're Voting On

yasindagli at gmail dot com
16 years ago
To create elements with attributes,

<?php

function createElement($domObj, $tag_name, $value = NULL, $attributes = NULL)
{
$element = ($value != NULL ) ? $domObj->createElement($tag_name, $value) : $domObj->createElement($tag_name);

if(
$attributes != NULL )
{
foreach (
$attributes as $attr=>$val)
{
$element->setAttribute($attr, $val);
}
}

return
$element;
}

$dom = new DOMDocument('1.0', 'utf-8');

$elm = createElement($dom, 'foo', 'bar', array('attr_name'=>'attr_value'));

$dom->appendChild($elm);

echo
$dom->saveXML();

?>

outputs :
<?xml version="1.0" encoding="utf-8"?>
<foo attr_name="attr_value">bar</foo>

<< Back to user notes page

To Top