Java : Convert formatted xml file to one line string

前端 未结 10 1359
时光取名叫无心
时光取名叫无心 2020-12-01 04:02

I have a formatted XML file, and I want to convert it to one line string, how can I do that.

Sample xml:



        
10条回答
  •  無奈伤痛
    2020-12-01 04:14

    // 1. Read xml from file to StringBuilder (StringBuffer)
    // 2. call s = stringBuffer.toString()
    // 3. remove all "\n" and "\t": 
    s.replaceAll("\n",""); 
    s.replaceAll("\t","");
    

    edited:

    I made a small mistake, it is better to use StringBuilder in your case (I suppose you don't need thread-safe StringBuffer)

提交回复
热议问题