package gao.javaweb.xml;
import java.io.File;
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 DomDemo {
public static void main(String args[]) throws Exception{
//(1)建DocumentBuilderFactory ,用于取得DocumentBuilder
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//(2)通过DocumentBuilderFactory 取得DocumentBuilder
DocumentBuilder doBuilder=null;
doBuilder = factory.newDocumentBuilder();
//(3)定义Document接口 ,通过DocumentBuilder类进行DOM树的转换操作
Document doc=null;
//指定XML的路劲
doc = doBuilder.parse("D:"+File.separator+"XML"+File.separator+"xml_demo.xml");
//(4) 查出name的节点
NodeList nl=doc.getElementsByTagName("linkman");
//(5) 输出NodeList第一个文本字节的 内容
for(int x=0;x<nl.getLength();x++){
Element e=(Element) nl.item(0);
System.out.println("姓名:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("公司:"+e.getElementsByTagName("company").item(0).getFirstChild().getNodeValue());
}
}
}