This function is buggy. It doesn't always return the correct line number, especially for text elements. As an alternative you can do something like this:
<?php
$text = $node->ownerDocument->saveXML($node);
$line += substr_count($text, "\n");
?>
You'll want to keep a reference to $line (starting at 0) and add to it as you parse over the document recursively.
In order for this to work you have to tell DOMDocument to preserve white space before loading the document.