2007-06-13 17:10:27
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class xukai_xml{
public static void main(String args[]){
try{
//工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//文档生成器
DocumentBuilder builder=factory.newDocumentBuilder();
//文档
Document doc=builder.parse("g:/xukai1.xml");
//节点树
NodeList nl =doc.getElementsByTagName("Row");
for (int i=0;i<nl.getLength();i++){
Element node=(Element) nl.item(i);
System.out.print("EMPNO: ");
System.out.println (node.getElementsByTagName("EMPNO").item(0).getFirstChild().getNodeValue());
System.out.print("ENAME: ");
System.out.println (node.getElementsByTagName("ENAME").item(0).getFirstChild().getNodeValue());
System.out.print("JOB: ");
System.out.println (node.getElementsByTagName("JOB").item(0).getFirstChild().getNodeValue());
System.out.print("MGR: ");
try
{
System.out.println (node.getElementsByTagName("MGR").item(0).getFirstChild().getNodeValue());
}catch(Exception e1)
{
System.out.println(" ");
}
System.out.print("HIREDATE: ");
System.out.println (node.getElementsByTagName("HIREDATE").item(0).getFirstChild().getNodeValue());
System.out.print("SAL: ");
System.out.println (node.getElementsByTagName("SAL").item(0).getFirstChild().getNodeValue());
System.out.print("COMM: ");
try
{
System.out.println (node.getElementsByTagName("COMM").item(0).getFirstChild().getNodeValue());
}catch(Exception e2)
{
System.out.println(" ");
}
System.out.print("DEPTNO: ");
System.out.println (node.getElementsByTagName("DEPTNO").item(0).getFirstChild().getNodeValue());
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}