I'm trying to pass a XML object through a StringBuilder to compare to Objects, fitting to my needs.
I was wondering if there is a nice way to remove specific tags from the whole String. Underneath I prepared an example:
Original:
<ApprovalSet>
<ApprovalItem application="Annotext" id="a089989361v451cag47e9f5e9a35716" name="ApprovalItemName" nativeIdentity="xxx12345" operation="Add" state="Finished" value="G1">
<ApprovalItemComments>
<Comment author="Random Guy" comment="THE NAME" date="1657122647591"/>
</ApprovalItemComments>
</ApprovalItem>
</ApprovalSet>
Desired Outcome:
<ApprovalSet>
<ApprovalItem application="Annotext" name="ApprovalItemName" nativeIdentity="xxx12345" operation="Add" value="G1"/>
</ApprovalSet>
So basically, I want to remove the id, state and the whole comment and close the ApprovalItem Tag (or just remove all slashes).
Any ideas? Thank you in advance :)
Jonas
Instead of string manipulation or regex, I would recomend to use a XML-parser. If all you need is to remove some elemnts and some attributes, you might want to look into Jsoup, which is actually a HTML-parser but can also handel XML and is very intuitive to work with. Using Jsoup your code could look like:
output
mvn dependency