I would like to delete a few Tags from my XML string. It sounds simple, and I'm sure it its, but I'm having problems deleting the main Tag and keeping the SubTags inside.
Here is an example:
<Tag1 version = "">
<SubTag1>
</SubTag1>
<SubTag2>
</SubTag2>
</Tag1>
I would like to keep only the "SubTag2" and everything inside it.
Which is the best way to approach this?
I tried using REGEX to delete SubTag1, but I found here that this is never a good solution in XML:
result = result.replaceAll("<SubTag1>[\\s\\S]*?</SubTag1>","");
Thank you in advance.
You could use JSoup (https://jsoup.org/) or another library to parse your string. Then you can access the DOM tree and extract only the part with the specific tag.
It would look like this:
If there is only one element with the tag "SubTag2" you can get access to it like this: