Question 1
Topic: Section 1 - DOM / SAX
Skill tested: Applies DOM node ownership rules when transferring nodes between documents.
A Java service assembles a report by parsing two XML files into separate DOM documents. It must move a selected element, including its descendants, from the second document into the first document. The implementation currently calls appendChild on the destination element and receives a WRONG_DOCUMENT_ERR. The team wants to preserve the selected subtree and its attributes, and does not need to retain the original node object or its identity. Which approach best meets these requirements?
A node created by one DOM Document cannot normally be inserted into a different document as-is. The destination document's importNode method creates a node owned by that destination; the true argument requests a deep copy of the subtree, after which it can be appended. normalize() concerns adjacent text nodes, not ownership. cloneNode() copies the subtree but does not make the clone belong to the destination document. adoptNode() is called on the document that will adopt the node, and adoption transfers rather than copies it; calling it on the source is incorrect and identity preservation is not required.