使用 Dom 进行解析会产生类型不匹配



给定以下代码,在Eclipse下,我得到一个类型不匹配错误:

package xmlInterface;

import javax.swing.text.*;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import gameManage.round;
import java.io.File;
import javax.lang.model.element.Element;
import javax.swing.text.Segment;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import com.sun.org.apache.bcel.internal.classfile.Method;



        public void writeToXml(round[] games) throws ParserConfigurationException
        {

                       int i;
                // build a doucument by the parser
                       DocumentBuilderFactory document = DocumentBuilderFactory.newInstance();
                       DocumentBuilder docBuilder = document.newDocumentBuilder();
                       Document doc = docBuilder.newDocument();
                       Element rootElement = doc.createElement("GameOut");
...
...
...
}

我在日食中收到以下错误:

Type mismatch: cannot convert from org.w3c.dom.Element to javax.lang.model.element.Element

任何人都可以解释一下我该如何解决这个问题吗?

谢谢杰森

我认为您误会了导入。不

import javax.lang.model.element.Element;

import org.w3c.dom.Element;

不要将导入与 * 一起使用

org.w3c.dom.*

否则,您可能会收到一些"隐藏"错误,因为您编码的最后一个"元素"导入(javax.lang.model.element.Element)将隐藏导入org.w3c.dom.*行中包含的org.w3c.dom.Element。

最新更新