简体   繁体   English

Java String Replace Xml标签,其中包含一些特定的单词

[英]Java String Replace Xml tag which has some specific word

I need to remove XML tag in a string which ends with "section". 我需要删除以“ section”结尾的字符串中的XML标记。 For example in the below XML string 例如在下面的XML字符串中

<OldSection>
    <sectionTitle>Sample Title</sectionTitle>
    <label> Hello Label </label>
    <heading>Hi </heading>
    <NewSection>
        <section>
            <InteractionSection>
                <sectionTitle>Section Title</sectionTitle>
                <label> Hello </label>
                <heading>Hi </heading>
                <para>
                    ...
                    ...
                </para>
            </InteractionSection>
        <section>
    </NewSection>
</OldSection>

I want to remove tags which ends with section ie <OldSection>, </OldSection> ,<NewSection></NewSection>, <InteractionSection>, </InteractionSection> etc. The tag alone should be removed and not the contents with in the tag. 我要删除以<OldSection>, </OldSection> ,<NewSection></NewSection>, <InteractionSection>, </InteractionSection>等部分<OldSection>, </OldSection> ,<NewSection></NewSection>, <InteractionSection>, </InteractionSection>的标签。应单独删除标签,而不要删除标签中的内容。标签。

I tried the following code but not working.. 我尝试了以下代码,但无法正常工作。

stringformat sf = new stringformat();

// REturns the xml string given as input 
String s = sf.getString(); 
String f = s; 

f = f.replaceAll("\\<*Section[^>].*?\\>", "");

Please any suggestions. 请任何建议。

Don't try to play with Strings using regex. 不要尝试使用正则表达式来玩Strings。 I would suggest that you do marshalling and unmarshalling. 我建议您进行编组和拆组。 Unmarshall your XML into a class. 将您的XML解组到一个类中。 Copy your requisite class contents into another class using Apache Commons' BeanUtils and then marshall this back into XML. 使用Apache Commons的BeanUtils将必需的类内容复制到另一个类中,然后将其编组回XML。

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

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