How to transform huge xml files in java?

前端 未结 7 1189
失恋的感觉
失恋的感觉 2021-01-12 07:04

As the title says it, I have a huge xml file (GBs)

  
  
     ...    
    ...          


        
7条回答
  •  一向
    一向 (楼主)
    2021-01-12 07:28

    Yes, just write a SAX content handler, and when it encounters a certain element, you build a dom tree on that element. I've done this with very large files, and it works very well.

    It's actually very easy: As soon as you encounter the start of the element you want, you set a flag in your content handler, and from there on, you forward everything to the DOM builder. When you encounter the end of the element, you set the flag to false, and write out the result.

    (For more complex cases with nested elements of the same element name, you'll need to create a stack or a counter, but that's still quite easy to do.)

提交回复
热议问题