Comparison between DOM mode and SAX mode

xiaoxiao2021-03-06  15

DOM mode

Package tigers;

Import java.util. *;

Import javax.xml.parsers. *; import org.w3c.dom. *; import java.io. *;

Public class tiger23 {static class bean23 {map attrs; // tag property String name, value; // tag name and text content public bean23 (String Name, String Value, Map attrs) { THIS.NAME = Name; this.Value = value; this.attrs = attrs;}

Public Map getattrs () {return attrs;}

Public void setattrs (map attrs) {this.attrs = attrs;

Public string getname () {return name;}

Public void setname (String name) {this.name = name;}

Public String getValue () {return value;}

Public void setvalue (string value) {this.value = value;} public string toString () {set keyset = attrs.keyset (); string str = ""; for (String key: keyset) {string value = Attrs.get (key); str = "" key "= '" value "";} return "/ will " Name ", <" value ">" ", (" STR ")> ";}} static map getElementatTRS (Element element) {map attrs = new hashmap (); if (Element.haSattributes () ) {NamedNodeMap Maps = Element.getattributes (); for (int i = 0; i getElements (Document Doc) {Collection Elements = new arraylist (); el ement rootElement = doc.getDocumentElement (); elements = circle (rootElement, elements); return elements;} static Collection circle (Element element, Collection elements) {String name = element.getTagName (); String value = ""; Map attrs = getElementattrs (element); bean23 bean = new bean23 (name, value, attrs); elements.add (bean); nodelist list =

Element.getchildNodes (); for (int i = 0; i parse (String xmlFile) {Collection elements = null; try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory. NewDocumentBuilder (); Document Doc = Builder.Pars (New File (XMLFile)); Elements = getElements (DOC);} catch (exception e) {E.PrintStackTrace ();} return elements;} public static Void main (string [] args) {Collection Elements = new arraylist (); long start = system.currenttimemillis (); for (int i = 0; i <100; i ) {Elements = PARSE "c: /test.xml"); system.gc ();} long end = system.currenttimemillis (); system.out.println (" PAST TIME:" (end - start); system .out.println (elements);}} SAX method

Package tigers;

Import java.util. *;

Import org.xml.sax.helpers.defaulthandler; import org.xml.sax.attributes; import org.xml.sax.xmlreader;

Import java.io. *; import org.xml.sax.inputsource; public class tiger22 {static class bean22 {// represents a tag in XML.

Private string Uri, LocalName, QNAME, Value = ""; Private Map attrs = new hashmap (); // current tag properties collection public bean22 (String Uri, String localname, string qname , Map attrs) {this.uri = URI; this.localname = localname; this.qname = qname; this.attrs = attrs;} public string getValue ()} public string getValue ()} public string getValue () PUBLIC SETVALUE value) {this.value = value;} public Map getAttrs () {return attrs;} public String getLocalName () {return localName;} public String getQName () {return qName;} public String getUri () {return uri;} Public string toString () {set keyset = attrs.keyset (); string str = ""; for (String Key: keyset) { String value = attrs.get (key); str = " key " = '" value " ";} return" / " " localname ", <" value ">" ", (" STR ")>";}} static class inner extends defaulthandler {private; // current tag private string text; // Current tag text content private collect Elements = new arraylist < Bean22> (); // Store all tag public collection

parse (String xmlFile) {try {XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader (); reader.setContentHandler (this); reader.parse (new InputSource (new FileReader (xmlFile)));} catch ( Exception e) {E.PrintStackTrace ();} return Elements;} / * Here you use java.util.map to store attribute information. Note: Org.xml.sax.attribute is originally a collection type, but due to SAX work, it is determined that if you use Attributes to store attribute information, the attribute information of the labeled tag may override the previously resolved marked attribute information, resulting in problems appear. So this will use MAP instead of Attributes to store attribute information, which can avoid the above problems. * / Public void startElement (String uri, String localName, String qName, Attributes attrs) {Map mapAttrs = new HashMap (); for (int i = 0; i

}}} Public static void main (string [] args) {Collection Elements = new arraylist (); long start = system.currenttimemillis (); for (int i = 0; i <100; i ) { Inner in = new inner (); elements = in.parse ("c: /test.xml"); system.gc ();} long end = system.currenttimemillis (); system.out.println (" PAST TIME: " (end - start)); system.out.println (Elements);}}

C: / Test.xml

bbbbbbbb 11223344 55667788

result:

Past Time: 5750 [Element: , ()>, element: , (id = '001')>, element: , (name = 'AAAA')>, Element: (name = 'bbbb')>, element: , ()>, element: , (ElementName = 'aaaa 'id =' 1234 ')>, element: , (ElementName =' aaaa 'id =' 5678 ')>]

result:

Past Time: 5907 [Element: , ()>, element: , (id = '001')>, element: , (name = 'AAAA')>, Element: (name = 'bbbb')>, element: , ()>, element: , (ElementName = 'aaaa 'id =' 1234 ')>, element: , (ElementName =' aaaa 'id =' 5678 ')>]

in conclusion:

1. Use any way, when the first parsing is time consuming, the average time used after continuous analysis of multiple times.

2, for small files, both can't see a big difference, SAX is even more time consuming than DOM; the larger the file, the greater the advantages of SAX.

3. In terms of programming difficulty, SAX is more easily implemented than DOM.

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

New Post(0)