解決dom4j加載xml文件性能慢的問題
阿新 • • 發佈:2018-10-27
tor style view play sqlmap XML common color gen
在代碼中使用:
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;View Codeimport 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。
解決dom4j加載xml文件性能慢的問題