XML-01總結

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();
		}
	}
}


結果:
姓名:林明
性別:男
年齡:26



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章