江湖戏子的专栏

江湖戏子的戏子江湖

查看xml子节点的父节点的属性

import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestClass {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String str = analysisHbmXml("..","_addtimes");
System.out.println(str);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static String analysisHbmXml(String calssnames,String columnName) throws DocumentException{
String classPath = "";
String classpaths=classPath+calssnames+".hbm.xml"; 
SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档  
        Document document = reader.read(new File(classpaths));
        String objName= null;//对象属性名
        String colName= null;//列名
Element rootElt = document.getRootElement();
        Iterator iter = rootElt.elementIterator("class");            
        while (iter.hasNext()) {  
            Element recordEle = (Element) iter.next(); 
            Iterator iditers = recordEle.elementIterator("id");
            while(iditers.hasNext()){
            Element idrecordEle1 = (Element) iditers.next();  
            Iterator iditerColumn = idrecordEle1.elementIterator("column");
            while(iditerColumn.hasNext()){
            Element idrecordEle2 = (Element) iditerColumn.next();  
            colName =   idrecordEle2.attributeValue("name");//elementTextTrim();
            if (colName.equals(columnName)) {
            objName = idrecordEle2.getParent().attributeValue("name");
}
            }
            }
            Iterator iters = recordEle.elementIterator("property");
            while(iters.hasNext()){
            Element recordEle1 = (Element) iters.next();  
            Iterator iterColumn = recordEle1.elementIterator("column");
            while(iterColumn.hasNext()){
            Element recordEle2 = (Element) iterColumn.next();  
            colName =   recordEle2.attributeValue("name");//elementTextTrim();
            if (colName.equals(columnName)) {
                objName = recordEle2.getParent().attributeValue("name");
}
            }
            }
        }  
        return objName;
}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hhl2046/article/details/46791191
个人分类: springMVC
上一篇Hadoop学习5
下一篇CKEditor使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭