Faster api than javax.xml.xpath to parse xml for value?

I use javax.xml.xpathto search for specific lines in xml files, however, due to the huge number of xml files that need to be searched, this is much slower than expected.

Is there any api supported by java that is faster than javax.xml.xpathor which is the fastest available?

+5
source share
4 answers

As indicated by skaffman , you must be sure that you are using libraries javax.xml.xpathas efficiently as possible. If you execute the XPath instruction more, as soon as you want to compile it into XPathExpression.

XPathExpression xPathExpression = xPath.compile("/root/device/modelname");
nl = (NodeList) xPathExpression.evaluate(dDoc, XPathConstants.NODESET);

Demo

# 2 , # 1.

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Demo {

    public static void main(String[] args) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            File xml = new File("input.xml");
            Document dDoc = builder.parse(xml);

            NodeList nl;

            // OPTION #1
            XPath xPath = XPathFactory.newInstance().newXPath();
            nl = (NodeList) xPath.evaluate("root/device/modelname", dDoc, XPathConstants.NODESET);
            printResults(nl);
            nl = (NodeList) xPath.evaluate("/root/device/modelname", dDoc, XPathConstants.NODESET);
            printResults(nl);

            // OPTION #2
            XPathExpression xPathExpression = xPath.compile("/root/device/modelname");
            nl = (NodeList) xPathExpression.evaluate(dDoc, XPathConstants.NODESET);
            printResults(nl);
            nl = (NodeList) xPathExpression.evaluate(dDoc, XPathConstants.NODESET);
            printResults(nl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printResults(NodeList nl) {
        for(int x=0; x<nl.getLength(); x++) {
            System.out.println("the value is: " + nl.item(x).getTextContent());
        }
    }

}

Input.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <blah>foo</blah>
  <device>
    <modelname>xbox</modelname>
  </device>
  <blah>bar</blah>
  <device>
    <modelname>wii</modelname>
  </device>
  <blah/>
</root>
+9

, XPath , XML? . , XML-, , , XML, , XPath/XQuery .

+4

- .

JXpath Xerces, Dom4J javax. , VTD-XML .

VTD-XML SO, .

EDIT:
ok, :

VTDGen vg = new VTDGen();
AutoPilot ap = new AutoPilot();
int i;
ap.selectXPath("/root/device/modelname");
if (vg.parseFile(PATH_TO_FILE,true)){
    VTDNav vn = vg.getNav();
    ap.bind(vn); // apply XPath to the VTDNav instance
    // AutoPilot moves the cursor for you
    while((i=ap.evalXPath())!=-1){
        System.out.println("the value is: " + vn.toNormalizedString(vn.getText()));
    }
}

XML:

<root>
  <blah>foo</blah>
  <device>
    <modelname>xbox</modelname>
  </device>
  <blah>bar</blah>
  <device>
    <modelname>wii</modelname>
  </device>
  <blah/>
</root>

:

the value is: xbox
the value is: wii

...

+1

, - Strings, , , Stax API (javax.xml.stream.XMLStreamReader). XPath , .

One of the problems with XPath is that, depending on the expression, it can lead to the creation of a DOM tree in memory, which is quite expensive (relative to XML parsing), both in terms of speed and memory usage. Therefore, if this can be avoided, it can speed up processing with a 3x factory.

0
source

All Articles