📄 dom.java
字号:
package com.demo.accp;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Dom {
public static void main(String args[]){
// file use candidate.xml
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("candidate.xml");//解析xml文件
System.out.println(doc.getImplementation());
NodeList nodes =doc.getElementsByTagName("PERSON");
for (int i=0;i<nodes.getLength();i++){
Element node=(Element) nodes.item(i);
System.out.print("NAME: ");
System.out.println (node.getElementsByTagName("NAME").item(0)
.getFirstChild().getNodeValue());
System.out.print("ADDRESS: ");
System.out.println (node.getElementsByTagName("ADDRESS").item(0)
.getFirstChild().getNodeValue());
System.out.print("TEL: ");
System.out.println (node.getElementsByTagName("TEL").item(0)
.getFirstChild().getNodeValue());
System.out.print("FAX: ");
System.out.println (node.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue());
System.out.print("EMAIL: ");
System.out.println (node.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue());
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -