次のような形式の 1000 エントリ ドキュメントがあります。
<Example>
<Entry>
<n1></n1>
<n2></n2>
</Entry>
<Entry>
<n1></n1>
<n2></n2>
</Entry>
<!--and so on-->
ここには 1000 を超えるエントリ ノードがあります。基本的にすべてのノードを1つずつ取得し、各ノードで分析を行うJavaプログラムを作成しています。しかし、問題は、ノードの検索時間がその数とともに増加することです。たとえば、最初のノードを取得するのに 78 ミリ秒かかり、2 番目のノードを取得するのに 100 ミリ秒かかり、増加し続けます。また、999 個のノードを取得するには 5 秒以上かかります。これは非常に遅いです。このコードを、1000 を超えるエントリを持つ XML ファイルにプラグインします。数百万のような人もいます。ドキュメント全体を解析する合計時間は 5 分以上です。
この単純なコードを使用してトラバースしています。これは、 xpathnxp
からノードを取得するためのすべてのメソッドを備えた独自のクラスです。
nxp.fromXpathToNode("/Example/Entry" + "[" + i + "]", doc);
ファイルのdoc
ドキュメントです。i
取得するノードの番号です。
また、私がこのようなことをしようとすると
List<Node> nl = nxp.fromXpathToNodes("/Example/Entry",doc);
content = nl.get(i);
私は同じ問題に直面しています。
ノードの tretirival を高速化する方法については誰もが解決策を持っているため、XML ファイルから 1 番目のノードと 1000 番目のノードを取得するのに同じ時間がかかります。
xpathtonode のコードは次のとおりです。
public Node fromXpathToNode(String expression, Node context)
{
try
{
return (Node)this.getCachedExpression(expression).evaluate(context, XPathConstants.NODE);
}
catch (Exception cause)
{
throw new RuntimeException(cause);
}
}
fromxpathtonodes のコードは次のとおりです。
public List<Node> fromXpathToNodes(String expression, Node context)
{
List<Node> nodes = new ArrayList<Node>();
NodeList results = null;
try
{
results = (NodeList)this.getCachedExpression(expression).evaluate(context, XPathConstants.NODESET);
for (int index = 0; index < results.getLength(); index++)
{
nodes.add(results.item(index));
}
}
catch (Exception cause)
{
throw new RuntimeException(cause);
}
return nodes;
}
そしてここからがスタート
public class NativeXpathEngine implements XpathEngine
{
private final XPathFactory factory;
private final XPath engine;
/**
* Cache for previously compiled XPath expressions. {@link XPathExpression#hashCode()}
* is not reliable or consistent so use the textual representation instead.
*/
private final Map<String, XPathExpression> cachedExpressions;
public NativeXpathEngine()
{
super();
this.factory = XPathFactory.newInstance();
this.engine = factory.newXPath();
this.cachedExpressions = new HashMap<String, XPathExpression>();
}