How to read and write xml files?

前端 未结 6 523
庸人自扰
庸人自扰 2020-11-22 09:03

I have to read and write to and from an XML file. What is the easiest way to read and write XML files using Java?

6条回答
  •  灰色年华
    2020-11-22 09:42

    The above answer only deal with DOM parser (that normally reads the entire file in memory and parse it, what for a big file is a problem), you could use a SAX parser that uses less memory and is faster (anyway that depends on your code).

    SAX parser callback some functions when it find a start of element, end of element, attribute, text between elements, etc, so it can parse the document and at the same time you get what you need.

    Some example code:

    http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

提交回复
热议问题