$dom = new DomDocument(); $root = $dom->createElement('Root'); $dom->appendChild($root); $item = $dom->createElement('Item'); $root->appendChild($item); echo $dom->saveXML();
$dom = new DomDocument(); $item = $dom->createElement('Item'); $item->setAttribute('id', '1'); $dom->appendChild($item); echo $dom->saveXML();In this example, we create a new element named "Item" and set the "id" attribute to 1. Then we add the element to the XML document and output it to the browser using the saveXML() method. Package library: DOM extension.