使用递归和dom遍历dom树形结构

使用递归逆推出xml文件的大体结构

package com.dowebber.xmlreader;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RecursionBasedDomXmlParser {


 public static void main(String[] args) {
  
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder builder = dbf.newDocumentBuilder();
   Document doc = builder.parse("src/xml/employee.xml");
   doc(doc);
   
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 private static void doc(Document doc){
  NodeList list = doc.getChildNodes();
  node(list);
 }
 
 private static void node(NodeList list){
  int len1 = list.getLength();
  if(len1 == 0){return;}
  for(int i=0;i<list.getLength();i++){
   Node node = list.item(i);
   if(node.getNodeType() == Node.TEXT_NODE){ //文本节点
    if(node.getTextContent().trim().length() > 0){
     System.out.println("#TEXT: " + node.getTextContent().trim());
    }
   }else if(node.getNodeType() == Node.COMMENT_NODE){ //注释节点
     System.out.println("<!--" + node.getTextContent().trim() + "-->");
   }else if(node.getNodeType() == Node.ELEMENT_NODE){
    if(node.hasChildNodes()){
     //考虑只有文本节点的情况<xxx>x</xxx>的情况
     int noneTextNodeCount = 0;
     NodeList childs = node.getChildNodes();
     for (int j = 0; j < childs.getLength(); j++) {
      if(childs.item(j).getNodeType() != Node.TEXT_NODE && node.getNodeType() != Node.COMMENT_NODE){
       noneTextNodeCount++;
      }
     }
     if(noneTextNodeCount != 0){
      System.out.print("<" + node.getNodeName() + " ");
      NamedNodeMap atts = node.getAttributes();
      if(atts != null){
       for (int j = 0; j < atts.getLength(); j++) {
        Node att = atts.item(j);
        if(att == null){continue;}
        System.out.print(att.getNodeName()+"='"+att.getNodeValue()+"' ");
       }
      }
      System.out.print(">");
      node(childs);
      System.out.print("</" + node.getNodeName() + ">");
      System.out.println();
     }else{
      //只有文本节点<xxx>x</xxx>
      System.out.print("<" + node.getNodeName() + " ");
      NamedNodeMap atts = node.getAttributes();
      if(atts != null){
       for (int j = 0; j < atts.getLength(); j++) {
        Node att = atts.item(j);
        if(att == null){continue;}
        System.out.print(att.getNodeName()+"='"+att.getNodeValue()+"' ");
       }
      }
      System.out.print(">");
      System.out.print(childs.item(0).getTextContent().trim());
      System.out.print("</" + node.getNodeName() + ">");
      System.out.println();
     }
    }else{
     //没有子节点 <xxx /> 这样的情况
     System.out.print("<" + node.getNodeName() + " ");
     NamedNodeMap atts = node.getAttributes();
     if(atts != null){
      for (int j = 0; j < atts.getLength(); j++) {
       Node att = atts.item(j);
       if(att == null){continue;}
       System.out.print(att.getNodeName()+"='"+att.getNodeValue()+"' ");
      }
     }
     System.out.print("/>");
     System.out.println();
    }
   }else if(node.getNodeType() == Node.DOCUMENT_TYPE_NODE){
    System.out.println("<!DOCTYPE " + node.getNodeName() + ">");
   }
   
  }
 }
 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值