dom4j解析带前缀名的xml文件(解析pdm文件)

   pdm文件的本质为xml文档,利用dom4j和xpath对pam文件解析,从而得到pdm文件的表和该表的列

Table.java---表的对象
package org.javaauto.bean;

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

public class Table {

	private String tableName;//表名
	
	private List<Column>columnList = new ArrayList<Column>();//字段对象

	public String getTableName() {
		return tableName;
	}

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

	public List<Column> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<Column> columnList) {
		this.columnList = columnList;
	}
	
	
}

column.java----列的对象
package org.javaauto.bean;

import java.math.BigDecimal;

public class Column {

	/**
	 * 数据的列
	 */
	
	
	private String columnType;//类型
	
	private String column;//字段
	
	private String columnName;//说明

	public String getColumnType() {
		if(columnType.toLowerCase().contains("varchar")||columnType.contains("char")){
			return "String";
		}
		else if(columnType.toLowerCase().contains("numeric")){
			return "BigDecimal";
		}
		else if(columnType.toLowerCase().contains("bigint")){
			return "Long";
		}
		else if(columnType.toLowerCase().contains("date")){
			return "Date";
		}
		else if(columnType.toLowerCase().contains("int")){
			return "int";
		}
		else{
			return columnType;
		}
		
	}

	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}	
}

解析文件XmlPrase.java 最后返回List<Table> 对象
package org.javaauto.xmlPrase;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.javaauto.bean.Column;
import org.javaauto.bean.Table;

public class XmlPrase {

	/**
	 * 解析xml文档,将解析到得数据封装到Table对象当中
	 */
	
	public List<Table> getXmlTable(String xmlPath){
		
		List <Table> tableList = new ArrayList<Table>();
		
		//根据xml文件,将文件已对象的形式表现出来
		File file = new File(xmlPath);
		//构建解析方式
		SAXReader reader = new SAXReader();
		//得到文档对象
		Document doc = null;
		try {
			doc = reader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.out.println("文档读取失败!");
			return null;
		}
		//根据xpath方式得到所要得到xml文档的具体对象,根据分析解析xml文档可知,xml文档中含有前缀名
		Map<String,String> map = new HashMap<String, String>();
		map.put("c","collection");
		//根据xml文档,//c:Table 即为得到的文档对象
		XPath path = doc.createXPath("//c:Tables");
		path.setNamespaceURIs(map);
		List<Element> list = path.selectNodes(doc);
		//得到tables对象,该对象是该pdm文件中所有表的集合
		for(Element element : list){
			for(Iterator<Element> iter = element.elementIterator("Table");iter.hasNext();){
				Element table_s = iter.next();
				Table table = new Table();
				table.setTableName(table_s.elementText("Code"));
				//通过一个table对象,得到columns对象
				Element columns = table_s.element("Columns");
				List <Column> columnList = new ArrayList<Column>();
				for(Iterator<Element> cols = columns.elementIterator("Column");cols.hasNext();){
					Element column = cols.next();
					Column column2 = new Column();
					column2.setColumn(column.elementText("Code"));
					column2.setColumnName(column.elementText("Name"));
					column2.setColumnType(column.elementText("DataType"));
					columnList.add(column2);
				}
				table.setColumnList(columnList);
				
				tableList.add(table);
			}
		}
		
		return tableList;
	}
	
	
	public static void main(String [] args){
		XmlPrase xp = new XmlPrase();
		xp.getXmlTable("e:/DRUC.xml");
		
	}
}



解析的xml文件为:
powerdesiner生成的pdm文件

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