fastest way to query xml in java

Mah*_*ngh 5 java xml dom xml-parsing

What is the fastest way to query a huge XML file in java,

DOM - xpath : this is taking lot of time,

     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
     docBuilderFactory.setNamespaceAware(true);

     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
     Document document = docBuilder.parse(new File("test.xml"));

     XPath xpath = XPathFactory.newInstance().newXPath();

     String xPath = "/*/*[@id='ABCD']/*/*";

     XPathExpression expr = xpath.compile(xPath);
     //this line takes lot of time
     NodeList result = (NodeList)expr.evaluate(document, XPathConstants.NODESET);
Run Code Online (Sandbox Code Playgroud)

with last line in code, program finishes in 40 secs and without it in 1 second.

SAX : I don't know if this can be used for query, on internet I am only able to find the examples of parsing.

What are the other options to make query faster, the size of my xml file is around 5MB. Thnx

Sur*_*tta 1

看一下SAX api,因为它是目前可用于处理 XML 文档的最快且占用内存最少的机制