I have a string whose content is an XML. I want to separate the tags and make it into a list of string in Java. Below is a something what I am trying:
string xml="<hello><hi a='a' b='b'/><hi a='b' b='a'/></hello>";
I want to separate it into a list like:
list[0]="<hi a='a' b='b'/>"
list[1]="<hi a='b' b='a'/>"
I tried to do this via JAXB processor but doesn't work well. Also tried some stupid logic using split but that didn't help either. Is there any other way to achieve this?
Although it's a bit unclear what you're trying to achieve, I wouldn't go for a full-blown XML parser in your case. With the standard
DOM
,SAX
orStax
parsers you will have to re-create your elements (esp. the attributes) or use aTransformer
.A simple
regex
seems to be the simplest solution here: