Which method in the DOM API is used to create a new element in an XML document?

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.

Reference links:

https://www.javaguides.net/p/java-xml-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top