当我试图获取根元素公司的属性时,我发现了以下问题,也有一些例外.
但我进口了我需要的一切; 然后eclipse说删除未使用的导入.
我想知道为什么即使在我导入了所有内容之后它也会发生,请给我一些想法来删除这个bug.
这也是做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 …Run Code Online (Sandbox Code Playgroud) 这对你们来说可能很简单,但由于我是java的新手,所以我想知道下一部分究竟发生了什么?
if (args.length > 0) {
file = args[0];
}
public class DomTest1 {
public static void main(String[] args) {
String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
}
}
Run Code Online (Sandbox Code Playgroud)