public void setFilePath(String filepath)throws Exception{
this.m_FilePath =filepath;
SAXBuilder sb = new SAXBuilder();
this.m_Document=sb.build(new FileInputStream(this.m_FilePath));
this.m_Root =this.m_Document .getRootElement() ;
}
/**
* return xml root
*/
public Element getRoot(){
return this.m_Root;
}
/**
* must have a model element
* @param parent
* @param childname -currentnode name
* @param table
*/
public void addChild(Element parent,String childname,Hashtable table){
Element CurrentNode=new Element(childname);
Enumeration enum=table.keys() ;
while (enum.hasMoreElements() ) {
String szkey=(String)enum.nextElement();
String szValue=(String)table.get(szkey);
Element node=new Element(szkey);
node.setText(szValue);
CurrentNode.addContent(node);
}
parent.addContent(CurrentNode);
}
/**
* not useful on the whole
* @param element
* @param attr
*/
public void setAttribute(Element element,Attribute attr){
element.addAttribute(attr);
}
/**
* end the operator
* @param outputfilepath
* @throws Exception
*/
public void write(String outputfilepath)throws Exception{
String indent = "\t";//小心,注意
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");
//outp.setTrimText(true);
outp.output(this.m_Document, new FileOutputStream(outputfilepath));
}
public void aaa(){
}
public static void main(String args[])throws Exception{
/**
* add content and add element
*//*
TestJDOM tj=new TestJDOM("bookList.xml");//bookList.xml
Element root=tj.m_Root ;
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
System.out.println("books: "+books.toString());
Element book = (Element)books.get(0); //得到第一个book元素(the first book)
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.addAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
System.out.println("author: "+author.getText());
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
//修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势
price.setText(Float.toString(50.0f));
Element phone =new Element("电话");
phone.setText("56384478");
book.addContent(phone);
Element salary =new Element("年薪");
//salary.setText("2万");
book.addContent(salary);
Element salary1 =new Element("年薪1");
salary1.setText("1.5万");
salary.addContent(salary1);
Element salary2 =new Element("年薪2");
salary2.setText("0.5万");
salary.addContent(salary2);
Element age =new Element("age");
age.setText("2年");
book.addContent(age);
tj.write("bookListOutPut.xml");
*/
/**
* <p>Title: This follow is newman's writing</p>
* <p>Description: I want better writing ,instead of best one!</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shu</p>
* @author Newman
* @version 1.0
*/
/**
* 查询,写入函数没有实现
* 觉得没有必要写这个类,实现sql查询太复杂了。
*/
/**
* This is the core class.
* Function:
* 1.It can read the file and input the content into the memory in the way of hashtable.
* Hashtable contains some vectors , just the columns.
* So the user can query the content in the certain column .
* 2.It also can save the changes back to the text.
* So the user can modify the content .
*/
public class TextSQL {
/**
* Load into memory
*/
public void loadInData() {
if(MyString.Assert(this.m_szFileName)&&MyString.Assert(this.m_szSeparator)){
FileReadWrite frw=new FileReadWrite(this.m_szFileName);
Enumeration enum=frw.getContentEnumeration();
boolean bflag=true;//create is true
while(enum.hasMoreElements()){
StringToken st=new StringToken((String)enum.nextElement(),this.m_szSeparator);
ArrayList list=new ArrayList();
list=st.workString();//get the data in line
if(list==null)//the file is null
return ;
int ColumnCount=list.size();
if(bflag){//at the first time
for(int i=0;i<ColumnCount;i++ ){
Vector v=new Vector();
this.m_Table .put(list.get(i),v);
this.m_vColumn .add(list.get(i)) ;//read the column
}
bflag=false;
}
else{
Enumeration enum2=this.m_Table.elements();
int i=0;
while(enum2.hasMoreElements()){
Vector v2=(Vector)enum2.nextElement();
v2.add(list.get(i));
i++;
}
}
}
}
else{
System.out.println("The database filename hasn't been defined!");
return;
}
}
/**
* set the separator
* @param separator
*/
public void setSeparator(String separator){
this.m_szSeparator =separator;
}
/**
* realize the queries
* @param column -the column
* @param command -int,string,boolean
* @return -result vector
*
* example:
-int :age<50 turn to float
-string:name='tom' , name like 'tom'
-boolean:gender=true
*
* ruler:select
*-----------------
id name age sex
1 tom 13 true
2 jack 23 false
3 rose 44 true
4 peter 22 false
5 kate 12 true
hashtable
id vectorid
1,2,3,4,5
name vectorname
tom,jack,rose,peter,kate
age vectorage
13,23,44,22,12
sex vectorsex
true,false,true,false true
/**
* get a recorder by index
* @param index the index of the vector
* @return vetor(a recorder)
*/
private Vector getRecorder(int index){
Vector v=new Vector();
Enumeration enum=this.m_Table.elements() ;
while(enum.hasMoreElements()){
Vector v2=(Vector)enum.nextElement() ;
v.add(v2.elementAt(index));
}
return v;
}
/**
* get printing string
* @param result -vector int vector,full records
* @return
*/
private String getPrintString (Vector result){
Enumeration enum=this.m_vColumn.elements();
String recordtitle="";
while(enum.hasMoreElements() ){
recordtitle=recordtitle+(String)enum.nextElement() +this.m_szSeparator;
}
recordtitle=recordtitle+this.m_ENTER;
String records="";//all records
int count=this.getRecordCount();
public String getType(){
return "";
}
//--------------------------------debug start
/**
* show all the data in the result
* @param result -vector int vector,full records
*/
private void printResult(Vector result){
String records=this.getPrintString(result);
System.out.println(records);
}
/**
* spy the data in the hashtable
*/
private void spyHashTable(){
Enumeration enum=this.m_Table.elements();
Vector vector=new Vector();
while(enum.hasMoreElements()){
Vector v=(Vector)enum.nextElement() ;
vector.add(v);
}
this.printResult(vector) ;
}
/**
* spy the data in the vector
*/
private void spyVector(Vector vector){
String records=this.getVectorString(vector);
System.out.println(records);
}
//-------------------------------------debug end
public static void main(String[] args) {
TextSQL textSQL1 = new TextSQL("TextSQL.txt"," ");
textSQL1.loadInData();
textSQL1.spyHashTable() ;//spy the hashtable
Vector v=textSQL1.getRecorder(2);
textSQL1.spyVector(v) ;//spy the vector