Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java解析Xml文件会引发异常。为什么?_Java_Xml_Xml Parsing - Fatal编程技术网

使用Java解析Xml文件会引发异常。为什么?

使用Java解析Xml文件会引发异常。为什么?,java,xml,xml-parsing,Java,Xml,Xml Parsing,当我试图获取根元素Company的属性时,我发现了以下问题,还有一些例外 但我进口了我需要的一切;然后eclipse还说删除未使用的导入 我想知道为什么在我把所有东西都导入之后, 请给我一些想法,以消除错误 这也是进行xml解析的方法吗?有没有其他简单的方法 你也这样做吗 import java.io.EOFException; import java.io.File; import javax.lang.model.element.Element; import javax.xml.parse

当我试图获取根元素Company的属性时,我发现了以下问题,还有一些例外

但我进口了我需要的一切;然后eclipse还说删除未使用的导入

我想知道为什么在我把所有东西都导入之后, 请给我一些想法,以消除错误

这也是进行xml解析的方法吗?有没有其他简单的方法 你也这样做吗

import java.io.EOFException;
import java.io.File;
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import javax.lang.model.element.Element;

public class DomTest1 {
    private static final String file = "test1.xml";

    public static void main(String[] args) {
        if (args.length>0) {
            file = args[0];
        }

        try {
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=factory.newDocumentBuilder();
            Document document=builder.parse(new File(file));
            Element root=document.getDocumentElement();
            System.out.println(root.getTagName());
            System.out.printf("name: %s%n", root.getAttribute("name"));
            System.out.printf("sHortname: %s%n", root.getAttribute("sHortname"));
            System.out.printf("mission : %s%n", root.getAttribute("mission"));
        } catch(EOFException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Type mismatch: cannot convert from org.w3c.dom.Element to javax.lang.model.element.Element
The method getTagName() is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element

at DomTest1.main(DomTest1.java:23)

问题就在于此

import javax.lang.model.element.Element;
使用此导入语句

import org.w3c.dom.Element
你有

import javax.lang.model.element.Element;
但是你想要

import org.w3c.dom.Element;
相反

此外,关于这一点:

这也是进行xml解析的方法吗?有没有其他选择和方法 这样做容易吗

import java.io.EOFException;
import java.io.File;
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import javax.lang.model.element.Element;

public class DomTest1 {
    private static final String file = "test1.xml";

    public static void main(String[] args) {
        if (args.length>0) {
            file = args[0];
        }

        try {
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
            DocumentBuilder builder=factory.newDocumentBuilder();
            Document document=builder.parse(new File(file));
            Element root=document.getDocumentElement();
            System.out.println(root.getTagName());
            System.out.printf("name: %s%n", root.getAttribute("name"));
            System.out.printf("sHortname: %s%n", root.getAttribute("sHortname"));
            System.out.printf("mission : %s%n", root.getAttribute("mission"));
        } catch(EOFException e) {
            e.printStackTrace();
        }
    }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Type mismatch: cannot convert from org.w3c.dom.Element to javax.lang.model.element.Element
The method getTagName() is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element

at DomTest1.main(DomTest1.java:23)

您正在使用java提供的开箱即用的方法进行xml解析。使用第三部分库有几个更好的选择。我建议进行测试,这要简单得多。参见示例。

那么我该怎么办?删除该导入?只需替换
import javax.lang.model.element.element
导入org.w3c.dom.Element您需要从org.w3c.dom.Element导入元素。。。“你有”它甚至两次“好的..但是现在在Element=document.getDocument元素行中。但是Eclipse建议我“向元素添加强制转换”为什么?错误在哪里?