Incomplete test of four XML analysis technology in Java

xiaoxiao2021-03-30  236

Test Environment: AMD Dragon 1.4G OC 1.5G, 256M DDR333, Windows2000 Server SP4, Sun JDK 1.4.1 Eclipse 2.1 Resin 2.1.8, test in Debug mode. The XML file format is as follows: a1234 XX Road XX Road XX, XX Road XX, XX, XX, Sichuan / Addr> b1234 Sichuan XX City XX Township XX Village XX Group Test Method: Let each The scheme is parsed by 10K, 100K, 1000K, 10000K XML file, calculates its consumption time (unit: milliseconds). JSP file: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "COM.TEST. *"%> <% string args [] = {" "}; Myxmlreader.main (args);%> test First, the DOM (JAXP CRIMSON parser) DOM is the official W3C standard of the XML document with the platform and language. The DOM is a collection of nodes or information tabs in hierarchical organizations. This hierarchy allows developers to find specific information in the tree. Analysis This structure typically needs to load the entire document and construct hierarchy before you can do anything. Since it is based on the information level, the DOM is considered to be trees or object-based. DOM and the generalized tree-based treatment have several advantages. First, since the tree is lasting in memory, it can modify it so that the application can make changes to the data and structure. It can also navigate up and down at any time, rather than being a one-time processing like SAX. DOM should be much simpler. On the other hand, for a particularly large document, parsing, and loading the entire document may be slow and very resource, so use other means to handle such data is better. These event-based models such as SAX.

BEAN file: package com.test; import java.io. *; Import java.util. *; Import org.w3c.dom. *; Import javax.xml.parsers. *; Public class myxmlreader {public static void main (String arge []) {long lasting = System.currentTimeMillis (); try {File f = new File ( "data_10k.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document doc = Builder.Parse (f); nodelist nl = doc.getElementsBytagname ("Value"); for (int i = 0; i

Due to its traversal capabilities, DOM parsers are often used in XML documents that require frequent changes. The SAX parser uses an event-based model. It can trigger a series of events when parsing the XML document. When a given TAG is found, it can activate a callback method, telling the label that the method has been found. SAX's requirements for memory are usually relatively low because it allows developers to determine the TAG to be processed. In particular, when the developer only needs to process some of the data contained in the document, SAX has a better manifestation. However, encoding work is more difficult to use the SAX parser, and it is difficult to access multiple different data in the same document. BEAN file: package com.test; import org.xml.sax. *; Import org.xml.sax.helpers. *; Import javax.xml.parsers. *; Public class myxmlreader Extends defaulthandler {java.util.stack tags = new java.util.Stack (); public MyXMLReader () {super ();} public static void main (String args []) {long lasting = System.currentTimeMillis (); try {SAXParserFactory sf = SAXParserFactory.newInstance (); SAXPARSER SP = sf.newsaxparser (); MyXMLReader Reader = new myXMLReader (); sp.parse ("DATA_10K.XML"), Reader;} catch (Exception E) {E.PrintStackTrace ();} system. Out.println ("Run Time:" (System.currentTimeMillis () - Lasting) "Mix");} public void characters (char ch [], int start, int length) throws saxexception {string tag = (String) Tags.peek (); if (tag.equals ("no")) {system.out.print ("license plate number:" new string (ch, start, length));} if (tag.equals ("AddR ")) {System.out.println (" Address: New String (CH, Start, Length));}} PUBLIC VOID Starlement (String Uri, ST Ring localname, string qname, attributes attrs) {tags.push (qname);}} 10k consumption time: 110 47 109 78 100K time consumption time: 344 406 375 422 1000K time consumption: 3234 3281 3688 3312 10000K time: 32578 34313 31797 31890 30328 Then the purpose of jdom http://www.jdom.org/ JDOM is to become a Java-specific document model, which simplifies interaction with XML and faster than using DOM.

Since it is the first Java specific model, JDOM has been vigorously promoted and promoted. It is considering that it is ultimately used as "Java Standard Extension" through "Java Specification Request JSR-102". JDM development has begun from early 2000. There are two main aspects of JDOM and DOM. First, JDOM only uses a specific class without using an interface. This simplifies API in some respects, but also limits flexibility. Second, the API uses a Collections class, simplifies the use of Java developers that are familiar with these classes. The JDOM document declares that its purpose is to "use 20% (or fewer) energy to solve 80% (or more) Java / XML issues" (assuming 20% ​​depending on the learning curve). Jdom is of course useful for most Java / XML applications, and most developers have found that API is much easier to understand than DOM. JDOM also includes a considerable extensive check of program behavior to prevent users from doing anything in XML. However, it still needs you to fully understand XML to do some work beyond basic work (or even understand in some cases). This may be more meaningful than learning the DOM or JDOM interface. JDOM does not contain a parser. It usually uses the SAX2 parser to parse and verify the input XML document (although it can also represent the previously constructed DOM as input). It contains some converters to indicate the JDOM to the SAX2 event stream, a DOM model, or an XML text document. JDOM is an open source released under the Apache license variant.

Bean file: package com.test; import java.io. *; Import java.util. *; Import org.jdom. *; Import org.jdom.input. *; Public class myxmlreader {public static void main (String Arge " ]) {long lasting = System.currentTimeMillis (); try {SAXBuilder builder = new SAXBuilder (); Document doc = builder.build (new File ( "data_10k.xml")); Element foo = doc.getRootElement (); List Allchildren = foo.getChildren (); for (int i = 0; i

Now you can see that more and more Java software is using DOM4J to read and write XML, especially worth mentioning that Sun's JAXM is also using DOM4J. Bean file: package com.test; import java.io. *; Import java.util. *; Import org.dom4j. *; Import org.dom4j.io. *; Public class myxmlreader {public static void main (String Arge " ]) {Long Lasting = system.currenttimemillis (); try {file f = new file ("DATA_10K.XML"); saxreader reader = new saxreader (); document doc = reader.read (f); element root = doc. GetrootElement (); element foo; for (iTerator i = root.elementiterator ("value"); I.hasNext ();) {foo = (element) i.next (); system.out.print ("license plate number: " foo.ElementText (" no ")); system.out.println (" owner address: " foo.ElementText (" addr "));}}} catch (exception e) {E.PrintStackTrace ();} System System .out.println ("Running time:" (system.currenttimemillis () - lasting) "milliseconds");}} 10k consumption time: 109 78 109 31 100K time consumption time: 297 359 172 312 1000k Time: 2281 2359 2344 2469 10000K Time: 20938 19922 20031 21078 The memory in the performance test during performance testing, memory overflow when testing 10m document. In the case of small documents, it is also worth considering using DOM and JDOM. Although JDOM developers have explained that they expect to focus on performance issues before formal release, it is indeed worth recommending. In addition, DOM is still a very good choice. The DOM implementation is widely used in a variety of programming languages. It is still the basis of many other standards related to XML because it officially gets W3C recommendation (relative to non-standard Java models), so it may also need it in some types of items (such as using DOM in JavaScript). SAX performance is better, which relies on its specific resolution. A SAX detection is coming upcoming XML stream, but does not load into memory (of course, some documents are temporarily hidden in memory when the XML stream is read. Undoubtedly, DOM4J is the winner of this test. At present, many of the open source projects use DOM4J, such as Ding Ding Hibernate, also use DOM4J to read XML configuration files. If the portability is not considered, the DOM4J bar related article: XML four parser principles and performance comparison

Eclipse 3.1.1 Chinese Pack Release MyEClipse 4.1 M2 Release

转载请注明原文地址:https://www.9cbs.com/read-130517.html

New Post(0)