在代码中使用:
1: DocumentHelper.parseText
2:
SAXReader reader = new SAXReader();
Document extdocument = reader.read(new File(extMapperPackagePath));
经过检测每次加载3KB文件的xml耗时1秒钟。性能极慢。经过很久终于找到原因,原来是验证xmlDTD文件导致的。
解决办法:跳过DTD验证。
步骤1:创建类
package com.my.common.generator.myplugins; import java.io.ByteArrayInputStream; import java.io.IOException; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class IgnoreDTDEntityResolver implements EntityResolver { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new ByteArrayInputStream("<?xml version=‘1.0‘ encoding=‘UTF-8‘?>".getBytes())); } }
步骤2:忽略验证
SAXReader reader = new SAXReader(false);
// 忽略DTD,降低延迟
reader.setEntityResolver(new IgnoreDTDEntityResolver());
Document extdocument = reader.read(new File(extMapperPackagePath));
另外注意:
DocumentHelper.parseText用以下代码代替
SAXReader reader = new SAXReader(false);
// 忽略DTD,降低延迟
reader.setEntityResolver(new IgnoreDTDEntityResolver());
Element baseMapperRootElement = reader.read(new InputSource(new StringReader(sqlMap.toString())))
说明:多次读取xml时可以复用SAXReader。
原文地址:https://www.cnblogs.com/wulm/p/9863104.html
时间: 2024-10-16 17:46:25