How to remove the white spaces between tags in XML

后端 未结 7 915
太阳男子
太阳男子 2020-12-06 18:00

I created an XML document using Java in my android application. I have to call a web service in my application and pass this XML as an argument there. But my problem is ther

7条回答
  •  鱼传尺愫
    2020-12-06 18:38

    This is the regular expression (?:>)(\s*)<

    When you use it in the code for Java use "(?:>)(\\s*)<" and replace with "><"

    String xmlString = "    Tove    JaniReminder Today    Don't forget me this weekend!    ";
    
    String str = xmlString.replaceAll("(?:>)(\\s*)<", "><");
    

    This will remove the spaces between the tags and maintain the spaces for the value.

    Input:

    
        Tove
        Jani
        Reminder Today
        Don't forget me this weekend!
    
    

    Output:

    ToveJaniReminderTodayDon't forget me this weekend!
    

提交回复
热议问题