Sure you can. Eg.

Copy<?php
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content');
$newsIntro->addAttribute('type', 'latest');
Header('Content-type: text/xml');
echo $newsXML->asXML();
?>

Output

Copy<?xml version="1.0"?>
<news newsPagePrefix="value goes here">
    <content type="latest"/>
</news>

Have fun.

Answer from DreamWerx on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ simplexmlelement.construct.php
PHP: SimpleXMLElement::__construct - Manual
See this example: <?php // This XML contains two elements called <child> // One is in the namespace http://example.com, with local prefix 'ws' // The other has no namespace (no prefix, and no default namespace declared) $xml = '<ws:example xmlns:ws="http://example.com"><child>Not in namespace</child><ws:child>In example namespace</ws:child></ws:example>'; $sx0 = new SimpleXMLElement($xml, 0, false); $sx1 = new SimpleXMLElement($xml, 0, false, 'http://example.com'); $sx2 = new SimpleXMLElement($xml, 0, false, 'ws', true); echo " Without: {$sx0->child} By namespace: {$sx1->child} By prefix: {$sx2->child} "; ?> Output: Without: Not in namespace By namespace: In example namespace By prefix: In example namespace
๐ŸŒ
BCCNsoft
doc.bccnsoft.com โ€บ docs โ€บ php-docs-7-en โ€บ simplexmlelement.construct.html
Creates a new SimpleXMLElement object
<?php $sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE); echo $sxe->asXML(); ?>
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_ref_simplexml.asp
PHP SimpleXML Functions
abstract and as break callable case catch class clone const continue declare default do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile extends final finally fn for foreach function global if implements include include_once instanceof insteadof interface isset list namespace new or print private protected public require require_once return static switch throw trait try use var while xor yield yield from PHP Libxml
๐ŸŒ
Regur
regur.net โ€บ blog โ€บ creation-xml-file-php-simplexml
Creation of XML File in PHP with SimpleXML - Regur Technology Solutions
October 6, 2016 - $xmlstr="<?xml version='1.0' encoding='UTF-8'?><news></news>"; //Creating SimpleXML Object $newsXML = new SimpleXMLElement($xmlstr); //Setting the newsPagePrefix attribute and its value to the news node $newsXML->addAttribute('newsPagePrefix', 'Times of India');
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-simplexmlelement__construct-function
PHP | SimpleXMLElement::__construct() Function - GeeksforGeeks
July 11, 2025 - <?php // Loading XML document to ... </user> XML; // Creating new SimpleXMLElement // object from $user $xml = new SimpleXMLElement($user); // Printing as XML echo $xml->asXML(); ?>...
๐ŸŒ
DEV Community
dev.to โ€บ eelcoverbrugge โ€บ php-simplexmlelement-gd1
PHP SimpleXMLElement - DEV Community
November 1, 2021 - $input = <<<end <?xml version='1.0' standalone='yes'?> <documents> <document> <name>spec.doc</name> </document> </documents> end; $xml = new simplexmlelement($input); $xml->document[0]->name = 'spec.pdf'; $output = $xml->asxml();
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ so it's possible for simplexmlelement to create invalid xml?
r/PHP on Reddit: So it's possible for SimpleXMLElement to create invalid XML?
November 11, 2014 -

So I was working on a way to convert arrays to XML strings using SimpleXML and felt confident that it would not error if attempting to create invalid XML, but apparently I'm wrong:

$xml = "<foo></foo>";
$xml = new \SimpleXMLElement($xml);
$xml->addChild(1,"bar");
$xml = $xml->asXML();
$valid = (bool) @simplexml_load_string($xml);
var_dump($xml, $valid);

outputs:

string(44) "<?xml version="1.0"?>
<foo><1>bar</1></foo>
"
bool(false)

I'm mainly posting to vent frustration and to let people know that they can't rely on SimpleXML to always create valid XML

Find elsewhere
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ beginners-guide-php-simplexml-library-xml-parsing
A Beginner's Guide to the PHP SimpleXML Library for XML Parsing | Reintech media
April 21, 2023 - function arrayToXml($data, $xml = null, $rootName = 'root') { if ($xml === null) { $xml = new SimpleXMLElement("<$rootName/>"); } foreach ($data as $key => $value) { // Handle attributes if (strpos($key, '@') === 0) { $xml->addAttribute(substr($key, 1), $value); continue; } // Handle text content if ($key === '_text') { $xml->{0} = $value; continue; } // Handle arrays and objects if (is_array($value)) { $child = $xml->addChild($key); arrayToXml($value, $child); } else { $xml->addChild($key, htmlspecialchars($value)); } } return $xml; } $jsonData = json_decode('{"product":{"@id":"123","name":"Laptop","price":"999.99"}}', true); $xml = arrayToXml($jsonData); echo $xml->asXML();
๐ŸŒ
Marcysutton
marcysutton.com โ€บ simple-xml-in-php
Simplexml in php 5 - MarcySutton.com
<?php $file = file_get_contents('navigation.xml'); $xml = new SimpleXMLElement($file); ?>