SimpleXMLElement finds nodes but DOMXPath doesn't

I don’t know why in this PHP function when I open the XML file with SimpleXMLElement I am able to get the nodes. But when I create a new DOMdocument and load the XML file into it, the DOMdocument isn’t created at all, so I cannot get the nodes.

<?php

function get_nodes() {
// load SimpleXML
/* $nodes = new SimpleXMLElement('communities.xml', null, true); */

$dom = new DOMDocument();
$dom->load('communities.xml');

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = true;

// get document element  

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY");

$nnodes = sizeof($nodes);
echo "<script type='text/javascript'> alert('Got ".$nnodes." nodes'); </script>";

foreach($nodes as $node) // loop through 
{

        echo "<div id = '".$node['ID']."' class= 'comdiv ui-widget-content' style = 'top: ".$node->TOP."; left: ".$node->LEFT."; width: ".$node->WIDTH."; 

height: ".$node->HEIGHT.";'> \
";            echo "   <p class = 'comhdr editableText ui-widget-header'>".$node->NAME."</p>\
";

        echo "   <a href='#' onClick=\\"delete_div('".$node['ID']."');\\">Delete</a>&nbsp;&nbsp;\
";
        echo "   <a href='#' onClick=\\"add_url('".$node['ID']."');\\">Add URL</a>&nbsp;&nbsp;\
";

        echo "</div> \
";

        echo "<script type='text/javascript'>\
";
        echo "  $('#".$node['ID']."').resizable();\
";
        echo "  $('#".$node['ID']."').draggable();\
";
        echo "  $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');\
";
        echo "</script>\
";

}
        echo "<script type='text/javascript'>\
";
        echo " 	$('.comhdr').editableText();\
";
        echo "</script>\
";

   return;
}

echo get_nodes();

?>