猪猪123 2012-03-27 15:24
浏览 582
已采纳

java解析xml文件多个线程同时访问时内存溢出

     java解析xml文件多个线程同时访问时内存溢出,代码如下:

解析的xml文件大小大概2M-10M,该怎么解决呢?

 

String sourceFile = ConfigReader.get("data.store.path") + article.getSourceFile();
        
        org.dom4j.Document document = null;
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(new File(sourceFile));
            document = new SAXReader().read(fin);
        } catch (final FileNotFoundException e) {
            final String msg = "内容单元xml文件不存在:" + sourceFile;
            log.error(msg, e);
            throw new RuntimeException(msg, e);
        } catch (final DocumentException e) {
            final String msg = "解析内容单元xml文件失败:" + sourceFile;
            log.error(msg, e);
            throw new RuntimeException(msg, e);
        } finally {
            IOUtils.closeQuietly(fin);
        }
        
        final StringBuilder xpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='")
            .append(article.getGuid()).append("']/").append("CONTENT");

        final Node node = document.selectSingleNode(xpath.toString());
        final String value = node.getText();
        article.setContent(value);
        
        final StringBuilder coordXpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='")
        .append(article.getGuid()).append("']/").append("COORDS");
        final Node coordsNode = document.selectSingleNode(coordXpath.toString());
        final String coordsValue = coordsNode.getText();
        article.setCoords(coordsValue);
        
        final StringBuilder briefXpath = new StringBuilder("/KFMP/DOCS/DOC[@GUID='")
        .append(article.getGuid()).append("']/").append("BRIEF");
        final Node briefNode = document.selectSingleNode(briefXpath.toString());
        final String briefValue = briefNode.getText();
        article.setBrief(briefValue);
        
        return article;
  • 写回答

7条回答 默认 最新

  • Tree_2002 2012-03-28 15:53
    关注

    建议使用sax解析 ,dom解析会将所有的数据读入内存 。

    或者进行增加硬件设施,或优化JVM等操作。

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?