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

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元