Found the culprit!
domDocument.load can work either asynchronously or synchronously. For
some reason, asynchronous work is the default.
The following line, inserted between the construction and the .load
operation eliminates the problem:
xdoc.async = false
Wonders of real-time processing!