关闭

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

697人阅读 评论(0) 收藏 举报
分类:
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;
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:79260次
    • 积分:1662
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:24篇
    • 译文:0篇
    • 评论:7条
    最新评论