Strip whitespace and newlines from XML in Java

后端 未结 6 2083
半阙折子戏
半阙折子戏 2020-12-01 21:01

Using Java, I would like to take a document in the following format:


 
    
 
         


        
6条回答
  •  醉梦人生
    2020-12-01 21:36

    As documented in an answer to another question, the relevant function would be DocumentBuilderFactory.setIgnoringElementContentWhitespace(), but - as pointed out here already - that function requires the use of a validating parser, which requires an XML schema, or some such.

    Therefore, your best bet is to iterate through the Document you get from the parser, and remove all nodes of type TEXT_NODE (or those TEXT_NODEs which contain only whitespace).

提交回复
热议问题