Search code examples
javaxmldomsaxstax

Does the complexity of XML structure has influence on parsing speed?


From "parsing speed" point of view, how much influence(if any) has number of attributes and depth of XML document on parsing speed? Is it better to use more elements or as many attributes as possible? Is "deep" XML structure hard to read?

I am aware that if I would use more attributes, XML would be not so heavy and that adapting XML to parser is not right way to create XML file

thanks


Solution

  • I think, it depends on whether you are doing validation or not. If you are validating against a large and complex schema, then proportionately more time is likely to be spent doing the validation ... than for a simple schema.

    For non-validating parsers, the complexity of the schema probably doesn't matter much. The performance will be dominated by the size of the XML.

    And of course performance also depends the kind of parser you are using. A DOM parser will generally be slower because you have to build a complete in-memory representation before you start. With a SAX parser, you can just cherry-pick the parts you need.


    Note however that my answer is based on intuition. I'm not aware of anyone having tried to measure the effects of XML complexity on performance in a scientific fashion. For a start, it is difficult to actually characterize XML complexity. And people are generally more interested in comparing parsers for a given sample XML than in teasing out whether input complexity is a factor.