java 运用dom解析xml文件之一.

xml文件如下:

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE Catalog [
 <!ENTITY dt "&#13;">
 <!ENTITY at "&#10;">
]>
<CHINAEXCEL>
<F1>
<General>
<DesignMode>0</DesignMode>
<FixedRows>7</FixedRows>
<GridColor>16777215</GridColor>
<ShowHeader>0</ShowHeader>
<TotalCol>4</TotalCol>
<TotalRow>39</TotalRow>
<UseAdo>1</UseAdo>
<VerMajor>4</VerMajor>
<VerMinor>0</VerMinor>
</General>
<DefaultCell>
<hAg>0</hAg>
<swty>0</swty>
<vAg>6</vAg>
</DefaultCell>
<CellList>
<WriteByRow>1</WriteByRow>
<Row1>
<Cou>4</Cou>
<CL2>
<Cols>1</Cols>
<FtId>13</FtId>
<Row>1</Row>
<Rows>1</Rows>
<hAg>6</hAg>
<t>供电生产调度情况</t>
</CL2>
<CL3>
<Cols>-2</Cols>
<FtId>5</FtId>
<Row>1</Row>
<Rows>-1</Rows>
<hAg>6</hAg>
<t>供电生产调度情况</t>
</CL3>
<CL4>
<Row>1</Row>
</CL4>
</Row1>
<Row2>
<Cou>4</Cou>
<CL2>
<Cols>-2</Cols>
<Row>2</Row>
<Rows>-1</Rows>
<hAg>6</hAg>
</CL2>
<CL3>
<Cols>-2</Cols>
<Row>2</Row>
<Rows>-1</Rows>
<hAg>6</hAg>
</CL3>
<CL4>
<Row>2</Row>
</CL4>
</Row2>
<Row4>
<Cou>4</Cou>
<CL1>
<FtId>6</FtId>
<Row>4</Row>
</CL1>
<CL2>
<FtId>6</FtId>
<Row>4</Row>
<hAg>2</hAg>
<t>200501</t>
</CL2>
<CL3>
<FtId>6</FtId>
<Row>4</Row>
<hAg>0</hAg>
<t>月份</t>
</CL3>
<CL4>
<FtId>6</FtId>
<Row>4</Row>
<hAg>6</hAg>
<t>ADLJ201号</t>
</CL4>
</Row4>
<Row5>
<Cou>4</Cou>
<CL1>
<FtId>6</FtId>
<Row>5</Row>
<hAg>6</hAg>
<t>部门:</t>
</CL1>
<CL2>
<FtId>6</FtId>
<Row>5</Row>
<hAg>0</hAg>
<t>一级部门</t>
</CL2>
<CL3>
<FtId>6</FtId>
<Row>5</Row>
<hAg>6</hAg>
</CL3>
<CL4>
<FtId>6</FtId>
<Row>5</Row>
<hAg>6</hAg>
<t>月后4日前报出</t>
</CL4>
</Row5>
<Row6>
<Cou>4</Cou>
<CL1>
<BPenId>0</BPenId>
<Cols>1</Cols>
<FtId>6</FtId>
<LPenId>0</LPenId>
<RPenId>0</RPenId>
<Row>6</Row>
<TPenId>0</TPenId>
<fl>241</fl>
<hAg>6</hAg>
</CL1>
<CL2>
<Cols>-1</Cols>
<FtId>6</FtId>
<Row>6</Row>
<Rows>-6</Rows>
<hAg>6</hAg>
</CL2>
<CL3>
<BPenId>0</BPenId>
<FtId>6</FtId>
<LPenId>0</LPenId>
<RPenId>0</RPenId>
<Row>6</Row>
<TPenId>0</TPenId>
<fl>241</fl>
<hAg>6</hAg>
<t>计算单位</t>
</CL3>
<CL4>
<BPenId>0</BPenId>
<FtId>6</FtId>
<LPenId>0</LPenId>
<RPenId>0</RPenId>
<Row>6</Row>
<TPenId>0</TPenId>
<fl>241</fl>
<hAg>6</hAg>
<t>本月</t>
</CL4>
</Row6>
..........
</CHINAEXCEL>

 

解析java类..

import javax.xml.parsers.*;
import org.w3c.dom.*;
import com.report.*;

public class dom1{
 public static String GetValueString(String filename,String row,String col){
   Node node5=null;
  try{
  

   DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
   DocumentBuilder builder=factory.newDocumentBuilder();
   
   Document doc=builder.parse(filename);
   Element element =doc.getDocumentElement();
   String row1="Row"+row;
   String col1="CL"+col;
  
   NodeList nodelist=element.getChildNodes();
   NodeList nodelist1=null;
   NodeList nodelist2=null;
   NodeList nodelist3=null;
   NodeList nodelist4=null;
   NodeList nodelist5=null;
   Node node=null;
   Node node1=null;
   Node node2=null;
   Node node3=null;
   Node node4=null;
   

   if(nodelist.getLength()>0)
   {
     
    node=nodelist.item(1);//f1
    nodelist1=node.getChildNodes();
    if(nodelist1.getLength()>4){
    
     
     node1=nodelist1.item(5);//node1对应celllist
     if(!node1.getNodeName().equals("CellList"))
      node1=nodelist1.item(7);
     
     
     nodelist2=node1.getChildNodes();
     for(int m=3;m<nodelist2.getLength();){
      node2=nodelist2.item(m);//要循环 ,获取行
      m=m+2;
    
      if(node2.getNodeName().equals(row1))
      {
       nodelist3=node2.getChildNodes();
    
       for(int j=3;j<nodelist3.getLength();){//对应列循环取
        node3=nodelist3.item(j);//获取 列
        j=j+2;
        if(node3.getNodeName().equals(col1))
        {
         nodelist4=node3.getChildNodes();
      
         for(int i=1;i<nodelist4.getLength();){
          node4=nodelist4.item(i);
      
          if(node4.getNodeName().equals("t"))
          {
           nodelist5=node4.getChildNodes();//对应的 值
        
           node5=nodelist5.item(0);
        
          }
       
         i=i+2;
      
         }
        
        }
      
       }    
    
      }
     }
    
    }
   
   }
   
 
  }catch(Exception e)
   {e.printStackTrace();
  }
   if(node5.getNodeValue()==null) return "0";
     else return node5.getNodeValue();
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值