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();
}
}
<?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();
}
}
相关文章推荐
- 运用JAVA读取txt文件
- java 读取properties文件,运用 使用properties
- Java实现文件的复制和新Nio包通道的运用--Thinking in java
- java客户端运用http向服务器批量传输文件
- Java文件运用
- java的简单反射运用实例(读取csv文件映射为对象)
- Java文件运用
- 运用JAVA读取txt文件
- javaIo——运用字节流完整复制文件夹及文件
- javaweb中运用fileupload上传文件
- Java运用流拆分文件
- Java File类学习笔记1:运用FileNameFilter类,获得某一目录下所有指定扩展名的文件
- 运用java.net.HttpURLConnection实现java多线程下载文件
- FCKeditor在java下的文件上传运用
- 【Java】运用多种方式读取文件内容,包括按字节、字符和按行为单位读取文件内容
- JavaIo——运用RandomAccessFile实现文件的多线程下载
- 运用ANT地SQL Task来完成自己地Java执行SQL脚本文件地功能
- 《java-----我的第一款像样的小软件》---运用了jsoup,jmail,多线程,文件的io操作等等
- 运用URLClassLoader加载外部jar包的java类,生成Class文件
- Java 字节流与字符流(综合运用:复制文件)