本文整理匯總了PHP中DomDocument::setAttribute方法的典型用法代碼示例。如果您正苦於以下問題:PHP DomDocument::setAttribute方法的具體用法?PHP DomDocument::setAttribute怎麽用?PHP DomDocument::setAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類DomDocument
的用法示例。
在下文中一共展示了DomDocument::setAttribute方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: createNodes
/**
* @param DomDocument $dom
* @param $data
* @param DomDocument $parent
*/
private function createNodes($dom, $data, &$parent)
{
switch (gettype($data)) {
case 'string':
case 'integer':
case 'double':
$parent->appendChild($dom->createTextNode($data));
break;
case 'boolean':
switch ($data) {
case true:
$value = 'true';
break;
case false:
$value = 'false';
break;
}
$parent->appendChild($dom->createTextNode($value));
break;
case 'object':
case 'array':
foreach ($data as $key => $value) {
if (is_object($value) and $value instanceof DOMDocument and !empty($value->firstChild)) {
$node = $dom->importNode($value->firstChild, true);
$parent->appendChild($node);
} else {
$attributes = null;
// SimpleXMLElements can contain key with @attribute as the key name
// which indicates an associated array that should be applied to the xml element
if (is_object($value) and $value instanceof SimpleXMLElement) {
$attributes = $value->attributes();
$value = (array) $value;
}
// don't emit @attribute as an element of it's own
if ($key[0] !== '@') {
if (gettype($value) == 'array' and !is_numeric($key)) {
$child = $parent->appendChild($dom->createElement($key));
if ($attributes) {
foreach ($attributes as $attrKey => $attrValue) {
$child->setAttribute($attrKey, $attrValue);
}
}
$this->createNodes($dom, $value, $child);
} else {
if (is_numeric($key)) {
$key = self::DEFAULT_XML_NODE;
}
$child = $parent->appendChild($dom->createElement($key));
if ($attributes) {
foreach ($attributes as $attrKey => $attrValue) {
$child->setAttribute($attrKey, $attrValue);
}
}
$this->createNodes($dom, $value, $child);
}
} else {
$parent->setAttribute(substr($key, 1), $value);
}
}
}
break;
}
}