DOMDocument::createElementNS
(PHP 5)
DOMDocument::createElementNS — Crea un nuevo nodo elemento con el nombre de espacio asociado
Descripción
Esta función crea un nuevo nodo elemento con el nombre de espacio asociado. Este nodo no se mostrará en el documento a no ser que sea insertado con (p.e.j.) DOMNode->appendChild().
Parámetros
- namespaceURI
-
El URI del nombre de espacio.
- qualifiedName
-
Elo nombre cualificado del elemento, como prefix:tagname.
- value
-
El valor del elemento. De manera predeterminada se creará un elemento vacio. El valor también puede ser asignado más tarde con DOMElement->nodeValue.
Valores devueltos
El nuevo DOMElement o FALSE si ha ocurrido un error.
Errores/Excepciones
- DOM_INVALID_CHARACTER_ERR
-
Lanzado si qualifiedName contiene un carácter inválido.
- DOM_NAMESPACE_ERR
-
Lanzado si qualifiedName es un nombre cualificado mal formado.
Ejemplos
Example #1 Crear un nuevo elemento e insertarlo como raíz
<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'This is the root element!');
// Insertamos el nuevo elemento como raíz (hijo del documento)
$dom->appendChild($element);
echo $dom->saveXML();
?>
El resultado del ejemplo sería:
<?xml version="1.0" encoding="utf-8"?> <xfoo:test xmlns:xfoo="http://www.example.com/XFoo">This is the root element!</xfoo:test>
Example #2 Un ejemplo de prefijo de nombre de espacio
<?php
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElementNS('http://www.w3.org/2005/Atom', 'element');
$doc->appendChild($root);
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
$item = $doc->createElementNS('http://base.google.com/ns/1.0', 'g:item_type', 'house');
$root->appendChild($item);
echo $doc->saveXML(), "\n";
echo $item->namespaceURI, "\n"; // Outputs: http://base.google.com/ns/1.0
echo $item->prefix, "\n"; // Outputs: g
echo $item->localName, "\n"; // Outputs: item_type
?>
El resultado del ejemplo sería:
<?xml version="1.0" encoding="utf-8"?> <element xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"> <g:item_type>house</g:item_type> </element> http://base.google.com/ns/1.0 g item_type
Ver también
- DOMNode::appendChild() - Añade un nuevo hijo al final de los hijos
- DOMDocument::createAttribute() - Crear nuevo attribute
- DOMDocument::createAttributeNS() - Crea un nuevo nodo atributo con un namespace asociado.
- DOMDocument::createCDATASection() - Crea un nuevo nodo cdata
- DOMDocument::createComment() - Crea un nuevo nodo de comentario
- DOMDocument::createDocumentFragment() - Crea un nuevo fragmento de documento
- DOMDocument::createElement() - Crea un nuevo nodo elemento
- DOMDocument::createEntityReference() - Create new entity reference node
- DOMDocument::createProcessingInstruction() - Crea un nuevo nodo PI
- DOMDocument::createTextNode() - Crea un nuevo nodo de texto
