Best method to parse various custom XML documents in Java

后端 未结 6 1023
死守一世寂寞
死守一世寂寞 2020-12-04 01:56

What is the best method to parse multiple, discrete, custom XML documents with Java?

6条回答
  •  既然无缘
    2020-12-04 02:18

    Basically, you have two main XML parsing methods in Java :

    • SAX, where you use an handler to only grab what you want in your XML and ditch the rest
    • DOM, which parses your file all along, and allows you to grab all elements in a more tree-like fashion.

    Another very useful XML parsing method, albeit a little more recent than these ones, and included in the JRE only since Java6, is StAX. StAX was conceived as a medial method between the tree-based of DOM and event-based approach of SAX. It is quite similar to SAX in the fact that parsing very large documents is easy, but in this case the application "pulls" info from the parser, instead of the parsing "pushing" events to the application. You can find more explanation on this subject here.

    So, depending on what you want to achieve, you can use one of these approaches.

提交回复
热议问题