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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵