How to remove the white spaces between tags in XML

后端 未结 7 906
太阳男子
太阳男子 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 = "<note>    <to>Tove</to>    <from>Jani</from <heading>Reminder</heading> <title>Today</title>    <body>Don't forget me this weekend!</body>    </note>";
    
    String str = xmlString.replaceAll("(?:>)(\\s*)<", "><");
    

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

    Input:

    <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading> <title>Today</title>
        <body>Don't forget me this weekend!</body>
    </note>
    

    Output:

    <note><to>Tove</to><from>Jani</from><heading>Reminder</heading><title>Today</title><body>Don't forget me this weekend!</body></note>
    
    0 讨论(0)
提交回复
热议问题