Java正则表达式删除xml结束标记中空格后的所有字符
我有一个以下格式的XML文件Java正则表达式删除xml结束标记中空格后的所有字符,java,regex,xml,Java,Regex,Xml,我有一个以下格式的XML文件 <?xml version="1.0" encoding="UTF-8"?> <SampleData ID="Test" Password="Test"> <STATUS operation=”remove”>EXPIRED</STATUS operation=”remove”> <PRIVILEGE operation=”remove”>12345</PRIVILEGE operation=”rem
<?xml version="1.0" encoding="UTF-8"?>
<SampleData ID="Test" Password="Test">
<STATUS operation=”remove”>EXPIRED</STATUS operation=”remove”>
<PRIVILEGE operation=”remove”>12345</PRIVILEGE operation=”remove”>
<userID>ABC123</userID>
<PROFILE operation=”remove”>DEFAULT</PROFILE operation=”remove”>
</SampleData>
期满
12345
ABC123
违约
在这个XML中,我不希望在空格后的end标记中有任何文本。例如,如果您考虑结束标签<代码> <代码>,我只希望它像“代码> <代码>显示。如果任何结束标记中没有空格,则该标记可以保持不变。此外,开始标记在任何情况下都将保持不变
有人能给我推荐一些正则表达式吗?我可以在这里解析整个XML并检查每个结束标记,这样我就可以去掉这些标记中空格后的任何字符。这里有一种方法可以做到这一点:
final String regex = "(<\\/.*)\\ (.*)>";
final String string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<SampleData ID=\"Test\" Password=\"Test\">\n"
+ "<STATUS operation=”remove”>EXPIRED</STATUS operation=”remove”>\n"
+ "<PRIVILEGE operation=”remove”>12345</PRIVILEGE operation=”remove”>\n"
+ "<userID>ABC123</userID>\n"
+ "<PROFILE operation=”remove”>DEFAULT</PROFILE operation=”remove”>\n"
+ "</SampleData>";
final String subst = "$1>";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println(result);
final String regex=“(”;
final String=“\n”
+“\n”
+“已过期\n”
+“12345\n”
+“ABC123\n”
+“默认值\n”
+ "";
最终字符串subst=“$1>”;
最终模式=Pattern.compile(regex);
final Matcher Matcher=pattern.Matcher(字符串);
//被替换的值将包含在结果变量中
最终字符串结果=matcher.replaceAll(subst);
系统输出打印项次(结果);
输出:
<?xml version="1.0" encoding="UTF-8"?>
<SampleData ID="Test" Password="Test">
<STATUS operation=”remove”>EXPIRED</STATUS>
<PRIVILEGE operation=”remove”>12345</PRIVILEGE>
<userID>ABC123</userID>
<PROFILE operation=”remove”>DEFAULT</PROFILE>
</SampleData>
期满
12345
ABC123
违约
在此处测试:多么奇怪的XML,我从未在结束标记中看到属性。如果不是,请不要称之为XML。处理声称是XML但不是XML的内容的最佳方法是修复创建它的程序中的错误。