2011-05-23 14 views
5

¿Qué debo hacer para agregar texto a un elemento XML? Por ejemplo:php DomDocument innerText

<videoTitle>I want to add text here</videoTitle> 

He creado el DOMDocument y he comenzado a agregar los elementos. Aquí está el elemento al que necesito agregar el texto.

$title = $vitals->appendChild($X->createElement("title")); 

Respuesta

4

Es necesario utilizar DOMDocument::createTextNode

$text = $X->createTextNode('Some text here'); 
$title->appendChild($text); 

Como alternativa, puede utilizar la sintaxis de acceso directo de DOMNode::$nodeValue:

$title->nodeValue = 'Some text here'; 

Hay que recordar con esta técnica que nodeValue conjuntos el contenido del texto, no el contenido XML. Las etiquetas se escapan, no se analizan.

+0

Hola, genial, gracias. Por cierto, ¿hay alguna forma en PHP para agregar automáticamente las etiquetas ? –

+0

@Lee No hay problema. Eche un vistazo a ['DOMDocument :: createCDATASection'] (http://php.net/manual/en/domdocument.createcdatasection.php). – lonesomeday