简体   繁体   English

使用Java删除数字xml标签

[英]remove numeric xml tag using java

I have the following xml: 我有以下xml:

<?xml version=\"1.0\"?>
<1>
<TITLE>A Sample Article</TITLE>
<SECT>The First Major Section      <PARA>This section will introduce a subsection.</PARA>
    <2>
    <SECT>The Subsection Heading         <PARA>This is the text of the subsection.         </PARA>      
    </SECT>
</SECT>
</ARTICLE>

I want to remove the numeric tags "<1>" and "<2>" using Java. 我想使用Java删除数字标签“ <1>”和“ <2>”。

Parsers won't work as its an invalid xml. 解析器将不起作用,因为它是无效的xml。 I need another solution such as a regular expression or any other idea. 我需要其他解决方案,例如正则表达式或其他任何想法。

You can just use the replaceAll method. 您可以只使用replaceAll方法。

String str = "YOUR XML HERE";
str = str.replaceAll("<[12]>", "");

IDEOne demo IDEOne演示

Or as Boheamian pointed in his comment you can use the \\d shortcut for digits: 或者,正如Boheamian在其评论中指出的那样,您可以使用\\d快捷方式输入数字:

str = str.replaceAll("<\\d>", "");

Btw, if you have more than <1> and <2> , like <n> being n whatever number, then you could use: 顺便说一句,如果您有多个<1><2> ,例如<n>n而不是n ,那么您可以使用:

str = str.replaceAll("<\\d+>", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM