Created
July 19, 2017 08:07
-
-
Save xinkunZ/469d8419f45b758d6d766394138133d2 to your computer and use it in GitHub Desktop.
XML读取
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hd123.test.util; | |
import java.io.File; | |
import java.util.Iterator; | |
import javax.persistence.Table; | |
import org.dom4j.Document; | |
import org.dom4j.DocumentException; | |
import org.dom4j.Element; | |
import org.dom4j.io.SAXReader; | |
import com.hd123.jpos.annotation.Remark; | |
import com.hd123.jpos.csi.entity.packageagency.PackAgencyReceiveTran; | |
/** | |
* | |
* @author zhangxinkun | |
*/ | |
public class GetTableName { | |
static String moved = "C:\\Users\\zxk84\\Desktop\\实体拆解\\moved.xml"; | |
static String to = "C:\\Users\\zxk84\\Desktop\\实体拆解\\to.xml"; | |
public static void main(String[] args) throws ClassNotFoundException, DocumentException { | |
PackAgencyReceiveTran tran = new PackAgencyReceiveTran(); | |
ClassLoader classLoader = tran.getClass().getClassLoader(); | |
SAXReader reader = new SAXReader(); | |
Document doc = reader.read(new File(moved)); | |
Element root = doc.getRootElement(); | |
Iterator<Element> i = root.elementIterator("class"); | |
StringBuilder sb = new StringBuilder("|| 类名 || 表名 || 含义 || 迁移到 ||\n"); | |
while (i.hasNext()) { | |
Element next = i.next(); | |
String className = next.getText(); | |
Class clazz = classLoader.loadClass(className); | |
String movedTo = getMovedTo(className); | |
className = clazz.getSimpleName(); | |
Table annotation = (Table) clazz.getAnnotation(Table.class); | |
Remark remark = (Remark) clazz.getAnnotation(Remark.class); | |
String tableName = annotation.name(); | |
String remarkName = "---"; | |
if (remark != null) { | |
remarkName = remark.text(); | |
} | |
sb.append(String.format("| %s | %s | %s | %s |", className, tableName, remarkName, movedTo)).append("\n"); | |
} | |
System.out.println(sb.toString()); | |
} | |
static String getMovedTo(String className) throws DocumentException { | |
String moved = ""; | |
SAXReader reader = new SAXReader(); | |
Document doc = reader.read(new File(to)); | |
Element root = doc.getRootElement(); | |
Iterator<Element> iterator = root.elementIterator(); | |
StringBuilder moveToStr = new StringBuilder(); | |
boolean alreadyFind = false; | |
while (iterator.hasNext()) { | |
Element next = iterator.next(); | |
String currentPersistenceVer = next.getName(); | |
Iterator<Element> classIter = next.elementIterator("class"); | |
while (classIter.hasNext()) { | |
Element i = classIter.next(); | |
if (className.equals(i.getText())) { | |
moveToStr.append(alreadyFind ? "," : "").append(currentPersistenceVer); | |
alreadyFind = true; | |
} | |
} | |
} | |
return moveToStr.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment