2012-07-10 56 views
0

我使用DOM下面使用网上的例子创建一个XML节点instread,XML DOM拥有CreateChild需要元素

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采取Node对象,而不是Element对象。我试图使用Node,但似乎没有暴露给set属性的方法,因此,我无法真正使用节点。

任何帮助将真的很感激它。

谢谢。

回答

3

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

无需输入org.w3c.dom.Element,因为doc.createElement("TEST")返回org.w3c.dom.Element的对象,该对象是org.w3c.dom.Node的子接口。

org.w3c.dom.Element rootElement = doc.createElement("TEST"); 
doc.appendChild(rootElement) 
+0

谢谢,它修复了它。 – Tony 2012-07-10 12:44:45