Which method in the DOM API is used to create a new element in an XML document?
A)
createElement()
B)
createNode()
C)
newElement()
D)
addElement()
Answer:
A)
createElement()
Explanation:
The createElement()
method in the DOM API is used to create a new element in an XML document. This method creates an element node with the specified name, which can then be appended to the document tree.
For example:
Document doc = ...; // Assume this is an existing Document object
Element newElement = doc.createElement("user");
doc.getDocumentElement().appendChild(newElement);
In this example, a new user
element is created and appended to the root element of the document.