The fastest way to query XML in Java

See English answer > efficient parser for large xmls4

DOM – XPath: this takes a 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);

On the last line of the code, the program completes in 40 seconds and does not have it in 1 second

Sax: I don't know if this can be used for query. I can only find examples of parsing on the Internet

What are the other options to make queries faster? The size of my XML file is about 5MB Day thnx

Solution

If your ID attribute is of type XS: ID, and your document has an XML schema, you can use document Getelementbyid (string) method I will give an example below

XML Schema

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <element name="foo">
        <complexType>
            <sequence>
                <element ref="tns:bar" maxOccurs="unbounded"/>
            </sequence>
        </complexType>
    </element>

    <element name="bar">
        <complexType>
            <attribute name="id" type="ID"/>
        </complexType>
    </element>

</schema>

XML input (input. XML)

<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns="http://www.example.org/schema">
    <bar id="ABCD"/>
    <bar id="EFGH"/>
    <bar id="IJK"/>
</foo>

demonstration

You need to set up a schema instance on documentbuilder factory to make everything work properly

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import org.w3c.dom.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File("src/forum17250259/schema.xsd"));

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setSchema(schema);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new File("src/forum17250259/input.xml"));

        Element result = document.getElementById("EFGH");
        System.out.println(result);
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>