digester

digester 解析xml
package com.zy.tools.queryframeconfig.bean;


import com.zy.queryframe.mapping.AbstractBean;
/**
 * 
 * @author Administrator
 * 创建时间 2010-6-10 上午 9:31
 */
public class QueryItem extends AbstractBean{
    private QueryResult queryResult = new QueryResult();
	private DeleteQuery deleteQuery = new DeleteQuery();
	private Columns  columns = new Columns();
	public QueryResult getQueryResult() {
		return queryResult;
	}
	public void setQueryResult(QueryResult queryResult) {
		this.queryResult = queryResult;
	}
	public DeleteQuery getDeleteQuery() {
		return deleteQuery;
	}
	public void setDeleteQuery(DeleteQuery deleteQuery) {
		this.deleteQuery = deleteQuery;
	}
	public Columns getColumns() {
		return columns;
	}
	public void setColumns(Columns columns) {
		this.columns = columns;
	}
}


package com.zy.tools.queryframeconfig.bean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.zy.queryframe.mapping.AbstractBean;
/**
 * 
 * @author Administrator
 * 创建时间 2010-6-10 上午 9:34
 */
public class Columns extends AbstractBean{
	@SuppressWarnings("unchecked")
	private List children = new ArrayList();
	private Column column;
	public List getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	
	public void addChildren(Column children) {
		this.children .add(children);
	}
	public Column getColumn() {
		return column;
	}
	public void setColumn(Column column) {
		this.column = column;
	}
}

package com.zy.tools.queryframeconfig.bean;

public class Column
{
	private Columns parent;
	//字段名称
	private String fieldName;
	//所属表
	private String tableName;
	
	//值表达式
	private String valueExpression;
	//查询表达式
	private String queryExpression;
	//名称表达式
	private String nameExpression;
	
	//是否支持查询
	private boolean suportQuery;
	
	//是否支持报表
	private boolean suportReport;
	
	//是否支持排序
	private boolean suportSort;
	
	//是否可见
	private boolean visible = true;
	
	//列宽
	private int width = 80;

	
	
	public Columns getParent() {
		return parent;
	}

	public void setParent(Columns parent) {
		this.parent = parent;
	}

	public String getFieldName()
	{
		return fieldName;
	}

	public void setFieldName(String fieldName)
	{
		this.fieldName = fieldName;
	}

	public String getTableName()
	{
		return tableName;
	}

	public void setTableName(String tableName)
	{
		this.tableName = tableName;
	}

	public String getValueExpression()
	{
		return valueExpression;
	}

	public void setValueExpression(String valueExpression)
	{
		this.valueExpression = valueExpression;
	}

	public boolean isSuportQuery()
	{
		return suportQuery;
	}

	public void setSuportQuery(boolean suportQuery)
	{
		this.suportQuery = suportQuery;
	}

	public boolean isSuportReport()
	{
		return suportReport;
	}

	public void setSuportReport(boolean suportReport)
	{
		this.suportReport = suportReport;
	}

	public boolean isSuportSort()
	{
		return suportSort;
	}

	public void setSuportSort(boolean suportSort)
	{
		this.suportSort = suportSort;
	}

	public boolean isVisible()
	{
		return visible;
	}

	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}

	public int getWidth()
	{
		return width;
	}

	public void setWidth(int width)
	{
		this.width = width;
	}

	public String getQueryExpression()
	{
		return queryExpression;
	}

	public void setQueryExpression(String queryExpression)
	{
		this.queryExpression = queryExpression;
	}

	public String getNameExpression()
	{
		return nameExpression;
	}

	public void setNameExpression(String nameExpression)
	{
		this.nameExpression = nameExpression;
	}
}

final StringBuffer sb = new StringBuffer();
		sb
		.append("\n\t").append(inputValue.getValue());

		String fileName = 
			"D:/B/S/ZYqueryframeconfigtool/src/com/zy/tools/queryframeconfig/xml/"+xmlName.getValue()+xmlType.getValue() ;
		try 
		{   
			FileWriter writer = new FileWriter( fileName);
			writer.append("<?xml version='1.0' encoding='utf-8'?>").append(sb);  
			writer.close();
		}
		catch ( IOException iox)
		{ 
			System.out.println("Problem writing" + fileName ); 
		}

		System.out.println("fieldName:"+sb);
		final Digester digester = new Digester();

		digester.addObjectCreate("query-item", QueryItem.class );
		digester.addSetProperties("query-item");
		
		digester.addObjectCreate("query-item/query", QueryResult.class);
		digester.addSetProperties("query-item/query");
		digester.addSetNext("query-item/query", "setQueryResult");
		
		
		digester.addObjectCreate("query-item/delete-query", DeleteQuery.class);
		digester.addSetProperties("query-item/delete-query");
		digester.addSetNext("query-item/delete-query", "setDeleteQuery");
		
		
		digester.addObjectCreate("query-item/columns", Columns.class);
		digester.addSetProperties("query-item/columns");
		digester.addSetNext("query-item/columns", "setColumns");
		
		digester.addObjectCreate("query-item/columns/column", Column.class );
        digester.addSetProperties("query-item/columns/column");
        digester.addSetNext("query-item/columns/column", "addChildren");
        
        
		
       
		QueryItem queryItem=(QueryItem) digester.parse(new File("D:/B/S/ZYqueryframeconfigtool/src/com/zy/tools/queryframeconfig/xml/"+xmlName.getValue()+".xml"));
	    System.out.println(((Column) queryItem.getColumns().getChildren().get(0)).getQueryExpression());
	    System.out.println(((Column)queryItem.getColumns().getChildren().get(1)).getQueryExpression());
	    List columnValue =(List) queryItem.getColumns().getChildren();
	    Executions.getCurrent().getDesktop().getSession().setAttribute("queryValue",columnValue );



<query-item
	id="" 
	queryType="sql"
	desc="">
	<query value="">
		<query-parameters>
		</query-parameters>
		<query-cache cacheable="false" cacheRegion=""/>
	</query>
	<delete-query tableName="">
	</delete-query>
	<columns>
		<column
			valueExpression=""
			nameExpression="dtproperties.id"
			queryExpression="p1.id"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.lvalue"
			queryExpression="p1.lvalue"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.objectid"
			queryExpression="p1.objectid"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.property"
			queryExpression="p1.property"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.uvalue"
			queryExpression="p1.uvalue"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.value"
			queryExpression="p1.value"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
		<column
			valueExpression=""
			nameExpression="dtproperties.version"
			queryExpression="p1.version"
			query="false"
			report="false"
			sort="false"
			visible="true"
			width="80px"/>
	</columns>
</query-item>

你可能感兴趣的:(java,bean,xml)