本文整理汇总了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;
}
}