本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0
 import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.xpath.*;

import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;


/**
* @author Peter (C)www.ldtec.com
*
* 处理xml配置文件
*
*/

public class SearchUtil {
private static String tablelistfile="C:/tomcat/webapps/csms/WEB-INF/search/tablelist.xml";

/**
* <p>通过类反射来获得xml文件的信息</p>
* @param vbean
* @param filepath
* @param xpath
* @return result
*/
public static List search(Class vbean,String xpath){
List result=new ArrayList();
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(tablelistfile);
root=doc.getRootElement();
List xlist=XPath.selectNodes(root,xpath);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int i=0;i<xlist.size();i++){
Element xele=(Element)xlist.get(i);
Object myclass=new Object();
myclass=Class.forName(vbean.getName()).newInstance();
for(int k=0;k<field.length;k++){
String vname=field[k].getName();
String method=vname.substring(0,1).toUpperCase()+vname.substring(1,vname.length());
String vmethod="set"+method;
Class[] clazz = new Class[1] ;
clazz[0] = String.class ;
Method met = Class.forName(vbean.getName()).getMethod(vmethod,clazz) ;
Object[] obj = new Object[1] ;
obj[0] = xele.getAttributeValue(field[k].getName());
Object ret = met.invoke(myclass,obj) ;
}
result.add(myclass);
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}

/**
* <p>xml文件路径从界面传进来..</p>
* @param vbean
* @param filepath
* @param xpath
* @return
*/
public static List search(Class vbean,String filepath ,String xpath){
List result=new ArrayList();
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(filepath);
root=doc.getRootElement();
List xlist=XPath.selectNodes(root,xpath);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int i=0;i<xlist.size();i++){
Element xele=(Element)xlist.get(i);
Object myclass=new Object();
myclass=Class.forName(vbean.getName()).newInstance();
for(int k=0;k<field.length;k++){
String vname=field[k].getName();
String method=vname.substring(0,1).toUpperCase()+vname.substring(1,vname.length());
String vmethod="set"+method;
Class[] clazz = new Class[1] ;
clazz[0] = String.class ;
Method met = Class.forName(vbean.getName()).getMethod(vmethod,clazz) ;
Object[] obj = new Object[1] ;
obj[0] = xele.getAttributeValue(field[k].getName());
Object ret = met.invoke(myclass,obj) ;
}
result.add(myclass);
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}

/**
* <p>xml文件路径由页面传进来</p>
* @param vbean
* @param filepath
* @param xpath
* @param map
* @return
*/
public static boolean modify(Class vbean,String filepath,String xpath,Map map){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(filepath);
root=doc.getRootElement();
List xlist=XPath.selectNodes(root,xpath);
Element xele=(Element)xlist.get(0);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int k=0;k<field.length;k++){
xele.setAttribute(field[k].getName(),(String) map.get(field[k].getName()));
}

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(filepath));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}


/**
* <p>修改某个节点</p>
* @param vbean
* @param xpath
* @return
*/
public static boolean modify(Class vbean,String xpath,Map map){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(tablelistfile);
root=doc.getRootElement();
List xlist=XPath.selectNodes(root,xpath);
Element xele=(Element)xlist.get(0);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int k=0;k<field.length;k++){
xele.setAttribute(field[k].getName(),(String) map.get(field[k].getName()));
}

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(tablelistfile));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}


/**
* <p>文件路径由页面传输进来!</p>
* @param vbean
* @param filepath
* @param xpath
* @param map
* @return
*/
public static boolean add(Class vbean,String filepath,String xpath,Map map,String strele){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{

doc=builder.build(filepath);
root=doc.getRootElement();
Element tablelistEle=(Element)XPath.selectSingleNode(root,xpath);
List xlist=tablelistEle.getChildren();
Element xele=new Element(strele);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int k=0;k<field.length;k++){
xele.setAttribute(field[k].getName(),(String) map.get(field[k].getName()));
}
xlist.add(xele);

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(filepath));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* <p>在当前xpath路径处,添加一个节点</p>
* @param vbean
* @param xpath
* @param map
* @return
*/
public static boolean add(Class vbean,String xpath,Map map,String strele){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{

doc=builder.build(tablelistfile);
root=doc.getRootElement();
Element tablelistEle=(Element)XPath.selectSingleNode(root,xpath);
List xlist=tablelistEle.getChildren();
Element xele=new Element(strele);
Field[] field=Class.forName(vbean.getName()).getDeclaredFields();
for(int k=0;k<field.length;k++){
xele.setAttribute(field[k].getName(),(String) map.get(field[k].getName()));
}
xlist.add(xele);

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(tablelistfile));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}

/**
* <p>文件路径由页面传进来</p>
* @param vbean
* @param filepath
* @param xpath
* @param map
* @return
*/
public static boolean remove(Class vbean,String filepath,String xpath,Map map){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(filepath);
root=doc.getRootElement();
List templist=XPath.selectNodes(root,xpath);
String key=(String)map.get("key");
String value="/(String)map.get(""value");
int row=0;
boolean flag=false;
for(int i=0;i<templist.size();i++){
Element tempele=(Element)templist.get(i);
if(tempele.getAttributeValue(key).equals(value)){
row=i;
flag=true;
}
}
if(flag==false){
return false;
}
int pos=0;
pos=xpath.lastIndexOf("/");
String vpath=xpath.substring(0,pos);
Element xele=(Element)XPath.selectSingleNode(root,vpath);
List xlist=xele.getChildren();
if(flag){
xlist.remove(row);
result=true;
}

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(filepath));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* <p>删除某个节点,map参数必须有一个键名为key和value</p>
* @param vbean
* @param xpath
* @param map
* @return result
*/
public static boolean remove(Class vbean,String xpath,Map map){
boolean result=false;
SAXBuilder builder=new SAXBuilder();
Document doc=null;
Element root=null;
try{
doc=builder.build(tablelistfile);
root=doc.getRootElement();
List templist=XPath.selectNodes(root,xpath);
String key=(String)map.get("key");
String value="/(String)map.get(""value");
int row=0;
boolean flag=false;
for(int i=0;i<templist.size();i++){
Element tempele=(Element)templist.get(i);
if(tempele.getAttributeValue(key).equals(value)){
row=i;
flag=true;
}
}
if(flag==false){
return false;
}
int pos=0;
pos=xpath.lastIndexOf("/");
String vpath=xpath.substring(0,pos);
Element xele=(Element)XPath.selectSingleNode(root,vpath);
List xlist=xele.getChildren();
if(flag){
xlist.remove(row);
result=true;
}

Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc,new FileOutputStream(tablelistfile));
result=true;
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String args[]){

Map kk=new HashMap();
kk.put("f1","xxxx");
kk.put("f2","MMMM");
kk.put("f3","JJJJ");
String v="kkk()";

String temp=(v.substring(0,1)).toUpperCase();
String vv=temp+v.substring(1,v.length());
System.out.println("temp->>"+temp);
System.out.println("vv->"+vv);
String path="c:/kk/lo.xml";
int start=path.lastIndexOf("/");
int end=path.lastIndexOf(".");
String xx=path.substring(start+1,end);
System.out.println("----xx->"+xx);
}

}
posted on 2006-10-13 09:12 有猫相伴的日子 阅读(1247) 评论(0)  编辑  收藏 所属分类: jdk

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网