Solve the problem of slow performance of loading XML files by dom4j

Source: Internet
Author: User

Use in the Code:

1: incluenthelper. parsetext

2:

Saxreader reader = new saxreader ();

Document extdocument = reader. Read (new file (extmapperpackagepath ));

It takes 1 second to load the XML file of 3 kb each time. Performance is extremely slow. After a long time, I finally found the cause, which was originally caused by the xmldtd file verification.

 

Solution: Skip DTD verification.

Step 1: Create a class

 Package  Com. My. Common. generator. myplugins;  Import  Java. Io. bytearrayinputstream; Import  Java. Io. ioexception;  Import  Org. xml. Sax. entityresolver;  Import  Org. xml. Sax. inputsource;  Import  Org. xml. Sax. saxexception;  Public   Class Ignoredtdentityresolver Implements  Entityresolver {@ override  Public Inputsource resolveentity (string publicid, string systemid)Throws  Saxexception, ioexception {  Return   New Inputsource ( New Bytearrayinputstream ("<? XML version = '1. 0' encoding = 'utf-8'?> " . Getbytes ()));}} 
View code

 

Step 2: Ignore Verification

Saxreader reader = new saxreader (false );
// Ignore DTD to reduce latency
Reader. setentityresolver (New ignoredtdentityresolver ());

Document extdocument = reader. Read (new file (extmapperpackagepath ));

 

Note:

Documenthelper. parsetext is replaced by the following code:

Saxreader reader = new saxreader (false );
// Ignore DTD to reduce latency
Reader. setentityresolver (New ignoredtdentityresolver ());

Element basemapperrootelement = reader. Read (New inputsource (New stringreader (sqlmap. tostring ())))

 

Note: saxreader can be reused when XML is read multiple times.

 

Solve the problem of slow performance of loading XML files by dom4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.