实习中遇到的第一个问题! finally version

来源:互联网 发布:矩阵测光特点 编辑:程序博客网 时间:2024/05/18 00:48

package com.hr;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Iterots {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try{
   DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
   DocumentBuilder db =factory.newDocumentBuilder();
   Document doc   =   db.parse(new File("MyXml.xml"));
   
   Element rootElement   =  doc.getDocumentElement();   
   System.err.println(doc.getDocumentURI());
   System.err.println(doc.getInputEncoding());
   System.err.println(doc.getXmlVersion());
   
         String rootName = rootElement.getNodeName();
        
         System.out.println("<"+rootName+">");
         Node rnode = rootElement.getChildNodes().item(1);
        
//         System.err.println(rnode.hashCode());
         NodeList list = rootElement.getElementsByTagName(rnode.getNodeName());
         System.err.println(rnode.getAttributes().getLength());
        
         print(list);
          
         System.out.println("</"+rootName+">");
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
 }
  
 static void print(NodeList list){
//  System.err.println(list.hashCode());
  for(int z=0;z<list.getLength();z++){
   Node node= list.item(z);   
   
//   判断此节点是否子节点
   if(node.hasChildNodes()){ 
    String nodeName = node.getNodeName();
    
    String name = node.getNodeName();
    NamedNodeMap atn = node.getAttributes();
    
    System.out.print("<"+name);
//    判断此节点是否有属性
    if(node.hasAttributes()){
         
            for(int x=0;x<atn.getLength();x++){
             Node anode = atn.item(x);
             System.out.print(" "+anode.getNodeName()+"="+anode.getNodeValue());
            }                         
        }
    System.out.println(">");  
        
    NodeList nl = node.getChildNodes();
    for(int i=0;i<nl.getLength();i++){
     Node na = nl.item(i);
     if(na.hasChildNodes()){
      NodeList l = na.getChildNodes(); 
      System.out.println("<"+na.getNodeName()+">");
      print(l);
      System.out.print(na.getFirstChild().getNodeValue());
      System.out.println("</"+na.getNodeName()+">");
     }
    }
    
    System.out.print(node.getFirstChild().getNodeValue());
    System.out.println("</"+nodeName+">");
   } 
  } 
 }
 
}

xml文件如下:

<?xml version="1.0" encoding="gbk"?>
<students>
 <student id="110" abc="cc">
  <name>李辉</name>
  <age>21</age>
  <sex>女</sex>
  <address>沈阳大东</address>
 </student>
 <student id="100">
  <name>zhangdan</name>
  <age>20</age>
  <sex>man</sex>
  <address>shen yan heping</address>
 </student>
 <student>
  <name>zhangdan</name>
  <age>23</age>
 </student>
</students>
还有一种处理方式。 下次接续......