$xml = new SimpleXMLElement(""); $xml->addChild("book"); $xml->book->addChild("title", "The Great Gatsby"); $xml->book->addChild("author", "F. Scott Fitzgerald"); $xml->saveXML("books.xml"); // saves the XML to a file
$xml = new SimpleXMLElement("This code creates a SimpleXMLElement object with a root element and adds a fruit element with a name and color child elements. The `saveXML()` method is called to save the XML element to a variable `$myXML` as a string. The `echo` statement is used to output the XML string. This method is part of the PHP core library, so no additional package library is required."); $xml->addChild("fruit"); $xml->fruit->addChild("name", "Apple"); $xml->fruit->addChild("color", "Red"); $myXML = $xml->saveXML(); // saves the XML as a string echo $myXML; // outputs the XML string