I am creating a XML using DOM as below using online examples,
DocumentBuilderFactory docfac= DocumentBuilderFactory.newInstance();
DocumentBuilder docb= docFactory.newDocumentBuilder();
Document doc = docb.newDocument();
// root
Element rootElement = (Element)doc.createElement("TEST");
doc.appendChild(rootElement); //Compiler error
...
appenchild takes Node object, not Element object. I was trying to use Node but, it seems like there is no methods exposed to set attribute, therefore, I can’t really use node.
Any help would be really appreciate it.
Thanks.
Please verify the packages you’ve imported :
import org.w3c.dom.Documentandimport org.w3c.dom.Element;and changedocfac.newDocumentBuilder();No need to type cast the
org.w3c.dom.Elementbecausedoc.createElement("TEST")returns an object oforg.w3c.dom.Elementwhich is a sub-interface oforg.w3c.dom.Node.