// creating a new element $newElement = new DOMElement('p'); $newElement->nodeValue = 'This is a new paragraph element.'; // getting an existing element to append to $existingElement = $dom->getElementById('existing-element'); // appending the new element to the existing element $existingElement->appendChild($newElement);
// getting an element to modify $element = $dom->getElementById('my-element'); // modifying the class attribute $element->setAttribute('class', 'new-class'); // removing a specific attribute $element->removeAttribute('data-attribute');These examples use the PHP built-in DOM extension.