코드가 너무 느리지 만 코드를 개선하는 방법을 잘 모르겠습니다. 디스크에서 DOM에 1k 파일을 읽는 데 약 20ms가 걸릴 수 있습니다. 디스크에 따라 다르지만 xpath 명령문에서 작업하는 데 20ms가 걸렸습니다. 너무 많이합니다. 다음은 시간 주석이있는 몇 가지 샘플 코드입니다. 코드를 개선하려면 어떻게해야합니까?DOM 및 XPath 최적화 Java 코드
이 건설 시간에 발생합니다
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = this.dbFactory.newDocumentBuilder();
XPathExpression[] ex = new XPathExpression[]{about 30 different expressions}
XPathExpression mainEx =xPath.compile("/rootElement/firstLevel/secondLevel");
를 다음 코드를 :
Document doc = this.dBuilder.parse("somefile.xml");
//took 20 ms until here
NodeList nodes = (NodeList) mainEx .evaluate,doc, XPathConstants.NODESET);
//took another 20 ms until here !!!
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
for (XPathExpression e:ex) {
String v = (String) e.evaluate(n, XPathConstants.STRING);
if (v != null) {
System.out.println(v);
}
}
}
//this only takes 5 ms
아마도 http://stackoverflow.com/questio와 관련이 있습니다. ns/6340802/java-xpath-apache-jaxp-implementation-performance –
VTD-XML으로 전환하십시오. <문제는 사라질 것입니다. –