I\'m creating a xml using this
$customXML = new SimpleXMLElement(\' \');
after adding some attributes onto this, when
In theory you can provide the LIBXML_NOXMLDECL option to drop the XML declaration when saving a document, but this is only available in Libxml >= 2.6.21 (and buggy). An alternative would be to use
$customXML = new SimpleXMLElement('<abc></abc>');
$dom = dom_import_simplexml($customXML);
echo $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
A practical solution: you know that the first occurrence of ?>
in the result string is going to be then end of the xml version substring. So:
$customXML = new SimpleXMLElement('<abc></abc>');
$customXML = substr($customXML, strpos($customXML, '?'.'>') + 2);
Note that ?>
is split into two parts because otherwise some poor syntax highlighter may have problems parsing at this point.
echo preg_replace("/<\\?xml.*\\?>/",'',$doc->saveXML(),1);
$customXML = new SimpleXMLElement('<source><abc>hello</abc></source>');
$result = $customXML->xpath('//abc');
$result = $result[0];
var_dump($result->asXML());
If this is a problem, this should do it:
$xml = str_replace(' version="1.0"', '', $xml);`
As SimpleXMLElement
always uses "\n"
to separate the XML-Declaration from the rest of the document, it can be split at that position and the remainder taken:
explode("\n", $customXML->asXML(), 2)[1];
Example:
<?php
$customXML = new SimpleXMLElement('<!-- some comment -->
<abc>
</abc>');
echo explode("\n", $customXML->asXML(), 2)[1];
Output:
<!-- some comment -->
<abc>
</abc>