1.Xml的主要作用是数据交换和系统配置。
2.dtd 语法规则 内部doctype 声明规则 对xml的验证
3.doc 解析
<?xml version="1.0" encoding="UTF-8"?>
<student>
<name id="001">林明</name>
<sex>男</sex>
<age>26</age>
</student>
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DOM01 {
public static void main(String[] args) {
DocumentBuilderFactory facotry=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder=facotry.newDocumentBuilder();
Document doc=builder.parse("src/Student.xml");
NodeList nodeList=doc.getElementsByTagName("student");
Element e=(Element)nodeList.item(0);
System.out.println("姓名:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("性别:"+e.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue());
System.out.println("年龄:"+e.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果: