ffhelly 2012-07-31 09:23
浏览 186
已采纳

关于dom4j 解析hbm.xml文件。。

用dom4j 解析 普通的XML速度倒是挺快的。。
但是解析hbm.xml的时候 时间就会很慢。。

我只是取其中的id的部分节点值 肿么可以更高效的读取呢??

现在读取的话 大概4个HBM.XML 需要2分多钟才能出来。。
而且2个HBM都不超过10个字段 没有set。
2个方法:
[code="java"]
//读取XML文件返回doc实体
public Document getDocumentByPath(String path){
SAXReader saxReader = new SAXReader();
Document document;
String end="";
try {
document = saxReader.read(path);
return document;
} catch (DocumentException e) {
e.printStackTrace();
return null;
}
}
//读取需要的节点值 并返回
public String getXmlValue(Document doc,String rootName){
SAXReader saxReader = new SAXReader();
Document document=doc;
String end="";
try {
//获取并返回所有对应节点的属性值
List list = document.selectNodes(rootName);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
end=attribute.getValue();
break;
}
return end;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
[/code]

求指点。。

  • 写回答

4条回答 默认 最新

  • libo_591 2012-07-31 14:47
    关注

    跳过DTD检查,部分代码如下:
    [code="java"]
    SAXBuilder sax = new SAXBuilder();
    sax.setValidation(false);
    sax.setEntityResolver(new EntityResolver(){//设置DTD验证
    public InputSource resolveEntity(String publicId, String systemId)
    throws FileNotFoundException {
    String localTLDPath = "";
    InputSource is = null;
    if(!"".equals(localTLDPath)){//设置了本地DTD验证
    is = new InputSource(new FileInputStream(localTLDPath));
    is.setPublicId(publicId);
    is.setSystemId(systemId);
    }else{//不验证
    is = new InputSource(new StringReader(""));
    }
    return is;
    }
    });
    result = sax.build(in);
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?