0

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.

4

1 回答 1

3

请验证您已导入的包:import org.w3c.dom.Documentimport org.w3c.dom.Element;更改docfac.newDocumentBuilder()

无需类型转换,org.w3c.dom.Element因为doc.createElement("TEST")返回的对象org.w3c.dom.Elementorg.w3c.dom.Node.

org.w3c.dom.Element rootElement = doc.createElement("TEST");
doc.appendChild(rootElement)
于 2012-07-10T03:04:16.057 回答