1

I need to remove XML tag in a string which ends with "section". For example in the below XML string

<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 i.e <OldSection>, </OldSection> ,<NewSection></NewSection>, <InteractionSection>, </InteractionSection> etc. The tag alone should be removed and not the contents with in the tag.

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.

4
  • 5
    Use SAX. Don't try to use regular expressions to crunch XML. Commented Sep 16, 2013 at 11:03
  • Yuo can use also XPAth library, run xml and look for nodes that you want to delete.maybe can i pass you and example, let me look for. Commented Sep 16, 2013 at 11:10
  • try this \\<.*Section> Commented Sep 16, 2013 at 11:46
  • In question like this you should inculde what exactly is not working. Otherwise people have to guess what your problem is. Commented Sep 16, 2013 at 12:25

1 Answer 1

1

Don't try to play with Strings using regex. I would suggest that you do marshalling and unmarshalling. Unmarshall your XML into a class. Copy your requisite class contents into another class using Apache Commons' BeanUtils and then marshall this back into XML.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.