java 解析pdm文档

前面展示了pdm 的xml结构,既然知道了结构,用java来解析也不会太难,这就为代码自动生成奠定了基础
package com.core.reader.pdmreader.imp;

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

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class PdmParser {

	public Table[] parsePDM_VO(String filePath) {
		Table[] tabs = new Table[] {};
		List voS = new ArrayList
(); Table vo = null; Column[] cols = null; File f = new File(filePath); SAXReader sr = new SAXReader(); Document doc = null; try { doc = sr.read(f); } catch (DocumentException e) { e.printStackTrace(); } Iterator itr = doc.selectNodes("//c:Tables//o:Table").iterator(); while (itr.hasNext()) { vo = new Table(); cols = new Column[] {}; List list = new ArrayList(); Column col = null; Element e_table = (Element) itr.next(); vo.setTableName(e_table.elementTextTrim("Name")); vo.setTableCode(e_table.elementTextTrim("Code")); Iterator itr1 = e_table.element("Columns").elements("Column").iterator(); while (itr1.hasNext()) { try { col = new Column(); Element e_col = (Element) itr1.next(); String pkID = e_col.attributeValue("Id"); col.setDefaultValue(e_col.elementTextTrim("DefaultValue")); col.setName(e_col.elementTextTrim("Name")); if(e_col.elementTextTrim("DataType").indexOf("(") >0){ col.setType(e_col.elementTextTrim("DataType").substring(0, e_col.elementTextTrim("DataType").indexOf("("))); }else { col.setType(e_col.elementTextTrim("DataType")); } col.setCode(e_col.elementTextTrim("Code")); col.setLength(e_col.elementTextTrim("Length") == null ? null : Integer.parseInt(e_col.elementTextTrim("Length"))); if(e_table.element("Keys")!=null){ String keys_key_id = e_table.element("Keys").element("Key").attributeValue("Id"); String keys_column_ref = e_table.element("Keys").element("Key").element("Key.Columns") .element("Column").attributeValue("Ref"); String keys_primarykey_ref_id = e_table.element("PrimaryKey").element("Key").attributeValue("Ref"); if (keys_primarykey_ref_id.equals(keys_key_id) && keys_column_ref.equals(pkID)) { col.setPkFlag(true); vo.setPkField(col.getCode()); } } list.add(col); System.out.println(col); } catch (Exception ex) { // col.setType(e_col.elementTextTrim("DataType")); System.out.println("+++++++++有错误++++" ); ex.printStackTrace(); } } vo.setCols(list.toArray(cols)); voS.add(vo); System.out.println(vo); System.out.println("======================"); System.out.println(); } return voS.toArray(tabs); } public static void main(String[] args) { PdmParser pp = new PdmParser(); Table[] tab = pp.parsePDM_VO("E:\\电子商务平台\\doc\\数据模型\\amysql.pdm"); pp.initTable(tab); } public void initTable(Table[] tabs) { List list = new ArrayList(); for (Table tab : tabs) { list.add(tab.getTableName()); System.out.println(tab.getTableName()); } // for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i)); // } } }

你可能感兴趣的:(java)