读xml平版文件(类似记集那样使用,功能还不是太完善) XMLlog4jJ#Apache
程序员文章站
2022-05-29 23:21:58
...
package xmlgrid;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.xpath.*;
import java.io.*;
import java.util.*;
import log.log4j29;
import org.apache.log4j.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ReadXml
{
static Logger debugLog = Logger.getLogger("debug");
static Logger infoLog = Logger.getLogger("info");
static Logger errorLog = Logger.getLogger("error");
private HashMap hsmap_IndexColumn = new HashMap(); //列序-列名对HashMap
private HashMap hsmap_ColumnIndex = new HashMap(); //列名-列序对HashMap
private Element em_root; //xml文档根元素element
private int curRowSequence; //当前行序号
private int rowCount; //总行数
private int columnCount; //列数
private ArrayList arr_CacheData; //缓冲数据
private int cacheRowCount; //缓冲行数
private int curCacheRow; //缓冲当前行的下标
private int curCacheRowOfD; //缓冲当前行所在记录集的下标
/**
*
* @param document
*/
public ReadXml(Document document)
{
PropertyConfigurator.configure("log4j.properties");
try
{
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = document; // 读入文件
// doc.toString();
// doc.
em_root = doc.getRootElement(); // 获得根元素element
//找到第一行
List list_find = XPath.selectNodes(em_root,
"/Grid/ROW[@RowSequence='1']");
// doc.get
//获取当前行
Element em_find = (Element) list_find.get(0);
//获取当前行的属性
List list_findchild = em_find.getAttributes();
for (int i = 0; i < list_findchild.size(); i++)
{
//获取第i列属性
Attribute att_column = (Attribute) list_findchild.get(i);
//将列的次序和列名放入hsmap_IndexColumn中
hsmap_IndexColumn.put("" + i, att_column.getName());
//将列名和列的次序放入hsmap_ColumnIndex中
hsmap_ColumnIndex.put(att_column.getName(), "" + i);
}
//设置总列数
columnCount = list_findchild.size();
//初始化当前行为第1行
}
catch (Exception e)
{
errorLog.error(e.toString());
}
}
//设置缓冲行数
public void setCacheRowCount(int CacheCount)
{
this.cacheRowCount = CacheCount;
}
public int getRowCount()
{
try
{
//获取总行数
List list_find = XPath.selectNodes(em_root, "/Grid/ROW");
debugLog.debug("find1.size():" + list_find.size());
this.rowCount = list_find.size();
return list_find.size();
}
catch (JDOMException e)
{
errorLog.error(e.toString());
return 0;
}
}
/**
*
* @return 列数
*/
public int getColumnCount()
{
return columnCount;
}
/**
*
* @param column_index
* @return //返回当前行第i列的值
*/
public String getString(int column_index)
{
String curRowData[] = (String[])this.arr_CacheData.get(this.curCacheRow);
return curRowData[column_index];
}
public void absolute(int RowIndex)
{
//如果指定行大于总行数或小于0,则让其行下标为指向最后一行
this.getRowCount();
if (RowIndex > rowCount || RowIndex < 0)
{
RowIndex = rowCount;
}
//如果没有指定缓冲行,则设置默认值
if (this.cacheRowCount == 0)
{
this.cacheRowCount = 10;
}
//设置为当前行
this.curRowSequence = RowIndex;
//缓冲开始行
int beginRow = calculateArea(RowIndex);
// System.out.println("开始行:" + beginRow);
//当前行所在缓冲区域的开始行
int curCacheBeginRow;
//缓冲当前行所在记录集的下标不为0时,计算其缓冲区开始位置
if (this.curCacheRowOfD != 0)
{
curCacheBeginRow = calculateArea(this.curCacheRowOfD);
}
//缓冲当前行所在记录集的下标为0时,缓冲区开始位置设为0
else
{
curCacheBeginRow = 0;
}
// System.out.println("当前行所在缓冲区域的开始行:" + curCacheBeginRow);
//缓冲结束行
int endRow = beginRow * this.cacheRowCount;
//System.out.println("结束行:" + endRow);
//准备缓冲开始行不等于当前缓冲开始行,则需要缓冲
if (beginRow != this.curCacheRowOfD)
{
//给当前缓冲开始行重新赋为正在缓冲的开始行
this.curCacheRowOfD = beginRow;
//缓冲数组列表
this.arr_CacheData = new ArrayList(this.cacheRowCount);
try
{
//查询
List list_find = XPath.selectNodes(em_root,
"/Grid/ROW[@RowSequence>='" +
beginRow +
"' and @RowSequence<='" + endRow +
"']");
// System.out.println("行数:" + list_find.size());
//填充数据到this.arr_CacheData中
for (int i = 0; i < list_find.size(); i++)
{
Element em_find = (Element) list_find.get(i);
//获取当前行的属性
List list_findAttributes = em_find.getAttributes();
String attributes[] = new String[list_findAttributes.size()];
for (int j = 0; j < list_findAttributes.size(); j++)
{
Attribute att_column = (Attribute) list_findAttributes.get(j);
attributes[j] = att_column.getValue();
// System.out.print(attributes[j]+" ");
}
// System.out.println();
this.arr_CacheData.add(attributes);
}
this.curCacheRow = (RowIndex - 1) % this.cacheRowCount;
//System.out.println("缓冲行1:" + curCacheRow);
}
catch (JDOMException e)
{
errorLog.error(e.toString());
}
}
//当前缓冲行与将缓冲行在相同的区域,则不需要重新缓冲
else if (beginRow == this.curCacheRowOfD)
{
this.curCacheRow = (RowIndex - 1) % this.cacheRowCount;
if (this.curCacheRow < 0)
{
this.curCacheRow = 0;
}
// System.out.println("当前缓冲行2:" + curCacheRow);
return;
}
}
public int getCurRowIndex()
{
return curRowSequence;
}
public boolean nextRecord()
{
if (curRowSequence + 1 > getRowCount())
{
return false;
}
else
{
++curRowSequence;
this.absolute(curRowSequence);
return true;
}
}
public String getString(String columnName)
{
return getString(getColunmIndex(columnName));
}
public String getColunmName(int ColunmIndex)
{
return (String) hsmap_IndexColumn.get("" + ColunmIndex);
}
public int getColunmIndex(String ColunmName)
{
Object cn = (Object) ColunmName;
return Integer.parseInt( (String) hsmap_ColumnIndex.get(ColunmName));
}
public static void main(String[] args)
{
WriteXml writeXml = new WriteXml(" (select UserID,'用户' as type,UserName,UserEmail1,UserStatus,OUID from TM_OUInfo,TM_GroupUserAndOU,TM_UserInfo where RelationType=3 and OUID=ObjectID and RelaObjectID=UserID "
+ " Union all select GroupID,'组' as type, GroupName,GroupEmail,'-1' as UserStatus,OUID from TM_OUInfo,TM_GroupUserAndOU,TM_GroupInfo "
+
" where RelationType=4 and OUID=ObjectID and RelaObjectID=GroupID)");
;
ReadXml readXml = new ReadXml(writeXml.writeXmlFile("company.xml"));
/*debugLog.debug("" + readXml.getRowCount());
readXml.getString(1);
debugLog.debug(readXml.getColunmIndex("UserLoginName") + "");
readXml.getString("UserLoginName");*/
int curIndex = readXml.getCurRowIndex();
while (readXml.nextRecord())
{
{
System.out.println(readXml.getString("RowSequence"));
}
}
}
public int calculateArea(int index)
{
int beginArea;
if (index % this.cacheRowCount != 0)
{
beginArea = (index / this.cacheRowCount) * this.cacheRowCount + 1;
}
else
{
beginArea = (index / this.cacheRowCount - 1) * this.cacheRowCount + 1;
}
return beginArea;
}
}
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.xpath.*;
import java.io.*;
import java.util.*;
import log.log4j29;
import org.apache.log4j.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ReadXml
{
static Logger debugLog = Logger.getLogger("debug");
static Logger infoLog = Logger.getLogger("info");
static Logger errorLog = Logger.getLogger("error");
private HashMap hsmap_IndexColumn = new HashMap(); //列序-列名对HashMap
private HashMap hsmap_ColumnIndex = new HashMap(); //列名-列序对HashMap
private Element em_root; //xml文档根元素element
private int curRowSequence; //当前行序号
private int rowCount; //总行数
private int columnCount; //列数
private ArrayList arr_CacheData; //缓冲数据
private int cacheRowCount; //缓冲行数
private int curCacheRow; //缓冲当前行的下标
private int curCacheRowOfD; //缓冲当前行所在记录集的下标
/**
*
* @param document
*/
public ReadXml(Document document)
{
PropertyConfigurator.configure("log4j.properties");
try
{
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = document; // 读入文件
// doc.toString();
// doc.
em_root = doc.getRootElement(); // 获得根元素element
//找到第一行
List list_find = XPath.selectNodes(em_root,
"/Grid/ROW[@RowSequence='1']");
// doc.get
//获取当前行
Element em_find = (Element) list_find.get(0);
//获取当前行的属性
List list_findchild = em_find.getAttributes();
for (int i = 0; i < list_findchild.size(); i++)
{
//获取第i列属性
Attribute att_column = (Attribute) list_findchild.get(i);
//将列的次序和列名放入hsmap_IndexColumn中
hsmap_IndexColumn.put("" + i, att_column.getName());
//将列名和列的次序放入hsmap_ColumnIndex中
hsmap_ColumnIndex.put(att_column.getName(), "" + i);
}
//设置总列数
columnCount = list_findchild.size();
//初始化当前行为第1行
}
catch (Exception e)
{
errorLog.error(e.toString());
}
}
//设置缓冲行数
public void setCacheRowCount(int CacheCount)
{
this.cacheRowCount = CacheCount;
}
public int getRowCount()
{
try
{
//获取总行数
List list_find = XPath.selectNodes(em_root, "/Grid/ROW");
debugLog.debug("find1.size():" + list_find.size());
this.rowCount = list_find.size();
return list_find.size();
}
catch (JDOMException e)
{
errorLog.error(e.toString());
return 0;
}
}
/**
*
* @return 列数
*/
public int getColumnCount()
{
return columnCount;
}
/**
*
* @param column_index
* @return //返回当前行第i列的值
*/
public String getString(int column_index)
{
String curRowData[] = (String[])this.arr_CacheData.get(this.curCacheRow);
return curRowData[column_index];
}
public void absolute(int RowIndex)
{
//如果指定行大于总行数或小于0,则让其行下标为指向最后一行
this.getRowCount();
if (RowIndex > rowCount || RowIndex < 0)
{
RowIndex = rowCount;
}
//如果没有指定缓冲行,则设置默认值
if (this.cacheRowCount == 0)
{
this.cacheRowCount = 10;
}
//设置为当前行
this.curRowSequence = RowIndex;
//缓冲开始行
int beginRow = calculateArea(RowIndex);
// System.out.println("开始行:" + beginRow);
//当前行所在缓冲区域的开始行
int curCacheBeginRow;
//缓冲当前行所在记录集的下标不为0时,计算其缓冲区开始位置
if (this.curCacheRowOfD != 0)
{
curCacheBeginRow = calculateArea(this.curCacheRowOfD);
}
//缓冲当前行所在记录集的下标为0时,缓冲区开始位置设为0
else
{
curCacheBeginRow = 0;
}
// System.out.println("当前行所在缓冲区域的开始行:" + curCacheBeginRow);
//缓冲结束行
int endRow = beginRow * this.cacheRowCount;
//System.out.println("结束行:" + endRow);
//准备缓冲开始行不等于当前缓冲开始行,则需要缓冲
if (beginRow != this.curCacheRowOfD)
{
//给当前缓冲开始行重新赋为正在缓冲的开始行
this.curCacheRowOfD = beginRow;
//缓冲数组列表
this.arr_CacheData = new ArrayList(this.cacheRowCount);
try
{
//查询
List list_find = XPath.selectNodes(em_root,
"/Grid/ROW[@RowSequence>='" +
beginRow +
"' and @RowSequence<='" + endRow +
"']");
// System.out.println("行数:" + list_find.size());
//填充数据到this.arr_CacheData中
for (int i = 0; i < list_find.size(); i++)
{
Element em_find = (Element) list_find.get(i);
//获取当前行的属性
List list_findAttributes = em_find.getAttributes();
String attributes[] = new String[list_findAttributes.size()];
for (int j = 0; j < list_findAttributes.size(); j++)
{
Attribute att_column = (Attribute) list_findAttributes.get(j);
attributes[j] = att_column.getValue();
// System.out.print(attributes[j]+" ");
}
// System.out.println();
this.arr_CacheData.add(attributes);
}
this.curCacheRow = (RowIndex - 1) % this.cacheRowCount;
//System.out.println("缓冲行1:" + curCacheRow);
}
catch (JDOMException e)
{
errorLog.error(e.toString());
}
}
//当前缓冲行与将缓冲行在相同的区域,则不需要重新缓冲
else if (beginRow == this.curCacheRowOfD)
{
this.curCacheRow = (RowIndex - 1) % this.cacheRowCount;
if (this.curCacheRow < 0)
{
this.curCacheRow = 0;
}
// System.out.println("当前缓冲行2:" + curCacheRow);
return;
}
}
public int getCurRowIndex()
{
return curRowSequence;
}
public boolean nextRecord()
{
if (curRowSequence + 1 > getRowCount())
{
return false;
}
else
{
++curRowSequence;
this.absolute(curRowSequence);
return true;
}
}
public String getString(String columnName)
{
return getString(getColunmIndex(columnName));
}
public String getColunmName(int ColunmIndex)
{
return (String) hsmap_IndexColumn.get("" + ColunmIndex);
}
public int getColunmIndex(String ColunmName)
{
Object cn = (Object) ColunmName;
return Integer.parseInt( (String) hsmap_ColumnIndex.get(ColunmName));
}
public static void main(String[] args)
{
WriteXml writeXml = new WriteXml(" (select UserID,'用户' as type,UserName,UserEmail1,UserStatus,OUID from TM_OUInfo,TM_GroupUserAndOU,TM_UserInfo where RelationType=3 and OUID=ObjectID and RelaObjectID=UserID "
+ " Union all select GroupID,'组' as type, GroupName,GroupEmail,'-1' as UserStatus,OUID from TM_OUInfo,TM_GroupUserAndOU,TM_GroupInfo "
+
" where RelationType=4 and OUID=ObjectID and RelaObjectID=GroupID)");
;
ReadXml readXml = new ReadXml(writeXml.writeXmlFile("company.xml"));
/*debugLog.debug("" + readXml.getRowCount());
readXml.getString(1);
debugLog.debug(readXml.getColunmIndex("UserLoginName") + "");
readXml.getString("UserLoginName");*/
int curIndex = readXml.getCurRowIndex();
while (readXml.nextRecord())
{
{
System.out.println(readXml.getString("RowSequence"));
}
}
}
public int calculateArea(int index)
{
int beginArea;
if (index % this.cacheRowCount != 0)
{
beginArea = (index / this.cacheRowCount) * this.cacheRowCount + 1;
}
else
{
beginArea = (index / this.cacheRowCount - 1) * this.cacheRowCount + 1;
}
return beginArea;
}
}
上一篇: ssm整合基本配置之springmvc
下一篇: webservice