$d = new DOMDOcument();
libxml_use_internal_errors(true);
$d->loadHTMLFile("http://stackoverflow.com");
$b = $d->getElementsByTagName("body")->item(0);
if ($b !== null) {
echo simplexml_import_dom($b)->asXML();
}
This will also include the <body>
tag, and the content will have been modified to be well-formed XML.
To have no body tags (though now we don't have a single root, thus not well-formed XML):
$d = new DOMDOcument();
libxml_use_internal_errors(true);
$d->loadHTMLFile("http://stackoverflow.com");
$b = $d->getElementsByTagName("body")->item(0);
if ($b !== null) {
for ($n = $b->firstChild; $n !== null; $n = $n->nextSibling) {
echo simplexml_import_dom($n)->asXML();
}
}