public static void main (String[] args) throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("config.xml");
//Element rootElement = doc.getDocumentElement();这句话如果不写我的程序运行没有问题,能够解析XML文件
NodeList lists=doc.getElementsByTagName("action");
for (int i = 0; i<lists.getLength(); i++){
Node node=lists.item(i);
Element ele=(Element)node;
String path=ele.getAttribute("path");
System.out.println (path);
NodeList lists2=ele.getElementsByTagName("forward");
for (int j = 0; j<lists2.getLength(); j++){
Node node2= lists2.item(j);
Element ele2=(Element)node2;
System.out.println (ele2.getAttribute("name"));
System.out.println (ele2.getFirstChild().getNodeValue());
}
}
System.out.println(lists.getLength());
}
我想问一下这句话Element rootElement = doc.getDocumentElement();是不是必须要加的,我发现不加程序也能运行,请问什么情况下必须加,什么情况下可以不加?