java读xml数据

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.isoftstone.fwk.util.XmlTool;

/**
 * 功能:
 * @author yaos  email: yaos@isoftstone-ai.com
 * @date 2008 11 25
 */
public class TestParseDOM {

 /**
  * 功能:
  * @param args
  * @throws TransformerException
  */

 
 
 public static void main(String[] args) throws Exception {
  //writeSchoolXml();
  readSchoolXml();
 }
 
 private static void readSchoolXml() throws Exception{
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  InputStream is = new FileInputStream(new File("src/writeXML.xml"));
  Document doc = db.parse(is);
  
  Element element = doc.getDocumentElement();
  NodeList rootNodeList = element.getChildNodes();
  //analysisNode(rootNodeList);
  /****************************************************************************************************/
  if(rootNodeList!=null){
   for(int i = 0 ; i < rootNodeList.getLength() ; i ++){
    Node node1 = rootNodeList.item(i);
    if(node1.getNodeType()==Node.ELEMENT_NODE){
     String name1 = node1.getAttributes().item(0).getNodeName();
     String value1 = node1.getAttributes().item(0).getNodeValue();
     System.out.println(name1+"-->"+value1);
     
     NodeList nodelist2 = node1.getChildNodes();
     if(nodelist2!=null){
      for(int j = 0 ; j < nodelist2.getLength() ; j++){
       Node node2 = nodelist2.item(j);
       if(node2.getNodeType()==Node.ELEMENT_NODE){
        String name2 = node2.getAttributes().item(0).getNodeName();
        String value2 = node2.getAttributes().item(0).getNodeValue();
        System.out.println(name2+"-->"+value2);
        
        NodeList nodelist3 = node2.getChildNodes();
        if(nodelist3!=null){
         for(int k = 0 ; k < nodelist3.getLength() ; k++){
          Node node3 = nodelist3.item(k);
          if(node3.getNodeType()==Node.ELEMENT_NODE){
           String childName = node3.getNodeName();
           String childValue = node3.getFirstChild().getNodeValue();
           System.out.println(childName+"-->"+childValue);
          }
         }
        }
       }
       System.out.println("------------");
      }
     }
    }
   System.out.println("------------");
   }
  }
  /****************************************************************************************************/
 }
 
 public static void analysisNode(NodeList nodelist){
  if(nodelist!=null){
   for(int k = 0 ; k < nodelist.getLength() ; k++){
    Node node = nodelist.item(k);
    if(node.getNodeType()==Node.ELEMENT_NODE){
     if(node.getAttributes()!=null&&node.getAttributes().item(0)!=null){
      String name2 = node.getAttributes().item(0).getNodeName();
      String value2 = node.getAttributes().item(0).getTextContent();
      System.out.println(name2+"-->"+value2);
     }else{
      String childName = node.getNodeName();
      String childValue = node.getFirstChild().getTextContent();
      System.out.println(childName+"-->"+childValue);
     }
     analysisNode(node.getChildNodes());
    }
   }
  }
 }
}

 

 

 

也可以将/***************/之间的去掉,调用递规函数analysisNode(NodeList nodelist)去解吸

方法中使用的xml的数据是 本BLOG中使用的 生成的school的xml数据及其结构

 

 

 

控制台应该输出:

1 . 调用递规函数输出为:

level-->1
name-->1
teacher-->teacher2
monitor-->stu2
number-->2
name-->2
teacher-->teacher3
monitor-->stu3
number-->4
level-->2
name-->1
teacher-->teacher3
monitor-->stu3
number-->4
name-->2
teacher-->teacher4
monitor-->stu4
number-->8
level-->3
name-->1
teacher-->teacher4
monitor-->stu4
number-->6
name-->2
teacher-->teacher5
monitor-->stu5
number-->12

 2 . 调用非递规函数输出为:

------------
level-->1
------------
name-->1
teacher-->teacher2
monitor-->stu2
number-->2
------------
------------
name-->2
teacher-->teacher3
monitor-->stu3
number-->4
------------
------------
------------
------------
level-->2
------------
name-->1
teacher-->teacher3
monitor-->stu3
number-->4
------------
------------
name-->2
teacher-->teacher4
monitor-->stu4
number-->8
------------
------------
------------
------------
level-->3
------------
name-->1
teacher-->teacher4
monitor-->stu4
number-->6
------------
------------
name-->2
teacher-->teacher5
monitor-->stu5
number-->12
------------
------------
------------
------------

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值