您的位置:首页 > 编程语言 > Java开发

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

2004-07-07 16:23 302 查看
xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE Catalog [
 <!ENTITY dt " ">
 <!ENTITY at " ">
]>
<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();
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息