兄弟有一个xml文本,样例如下:
<cc>
    <c code="3">100</c>
    <c code="5">100</c>
</cc>这个文本如果去掉换行符,则是
<cc>    <c code="3">100</c>    <c code="5">100</c></cc>我想用正则表达式去掉<cc>    <c code="3">和</c>    <c code="5">之间的空格,但是不知道怎么写,求教大家了。

解决方案 »

  1.   

    String a="E:/A S3/pengqi/Shoping/src/ckstudio/db/"; a.replaceAll("/","\\");不知道合不合适你的要求
      

  2.   

    public class Replacement {    public static void main(String[] args) {
            String str =
                "<cc>\r\n" +
                "  <c code=\"3\">100</c>\r\n" +
                "  <c code=\"5\">100</c>\n" +
                "</cc>";
            
            System.out.println(str);
            System.out.println();
            
            str = str.replaceAll("(?<=>)\\s+(?=<)", "");
            System.out.println(str);
        }
    }
      

  3.   

    为了不把标签中元素中纯空格替换掉的话(像下面 /cc/c[@code='5'] 中的空格),可以改成这样:public class Replacement {    public static void main(String[] args) {
            String str =
                "<cc>\r\n" +
                "  <c \n" +
                "  \r\n\t" +
                "\r\n" +
                "\t  code=\"3\">   \n" +
                "   100\t   \r\n" +
                " \n" +
                "  </c>\r\n" +
                "  <c code=\"5\"> </c>\t\n" +
                "\n  \t\n\t" +
                "</cc>";
            
            System.out.println(str);
            System.out.println();
            
            str = str.replaceAll("[\\s&&[^\r\n]]*(?:[\r\n][\\s&&[^\r\n]]*)+", "");
            System.out.println(str);
        }
    }
      

  4.   

    xml
    文件 转为字符串 在通过上面的方法试试
      

  5.   

    如果在[!CDATA]里面有">   "呢?
      

  6.   

    String str = " hell  w  or    d   !  !  !";String str2 = str.replaceAll("\\s", "");System.out.println(str2);结果是"hellword!!!.replaceAll("\\s", ""));//可以替换所有空白字符, 不限于空格