Parsing XML in Java. DOM vs. Sax

    Finally got around to write this note.
    In a previous article, I wrote about creating the simplest xml parser using SAX technology.
    At the request of the workers, he made the next issue of this series. SAX and DOM performance comparison.

    So, let me remind you.
    We had an xml file, which is a data structure that describes a doctor.
    Here she is:


    3
    Сидоров
    Семен
    Семенович
    12-05-1976
    13-04-2005
    02-03-2004
    18-06-2009
    22-01-2022
    1
    1
    1
    1.0
    1


    * This source code was highlighted with Source Code Highlighter.


    All this miracle weighs 411 bytes.
    I decided to make a test simple. Parsed this file in a loop N times.
    Let's look at the chart.
    image
    Note: vertically we have time in milliseconds, horizontally marks with the amount of information (format N * 411, where N is the number of times the xml file was run).

    We get a curious addiction. The greater the amount of processed information, the longer the DOM starts to work. Against this background, SAX looks very advantageous if you need speed with large volumes.

    In any case, I do not want to draw any conclusions, so as not to breed holivars. I can only say that in choosing a method you need to use common sense and set goals.

    Also popular now: