Hello Man
Hello Man

Reputation: 701

Parse XML string and build a list of strings

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?

Upvotes: 3

Views: 5994

Answers (2)

nyname00
nyname00

Reputation: 2566

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 or Stax parsers you will have to re-create your elements (esp. the attributes) or use a Transformer.

A simple regex seems to be the simplest solution here:

String xml = "<hello><hi a='a' b='b'/><hi a='b' b='a'/></hello>";
String[] es = xml.split("(?=<)|(?<=>)");
List<String> result = new ArrayList<>(es.length);
for (int i = 0; i < es.length; i++) {
    // do not add first and last element ("hello" in your example)
    if (i > 0 && i < es.length - 1)
       result.add(es[i]);
}

Upvotes: 0

Mahdi
Mahdi

Reputation: 2279

string xml="<hello><hi a='a' b='b'/><hi a='b' b='a'/></hello>";

//read XML from the given string
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);

//this will return a list of xml tags whose name is `hi`
NodeList hiList = document.getElementsByTagName("hi");

//you can iterate over hiList and read/process them
for (int i = 0; i < hiList.getLength(); i++) {
    Node child = hiList.item(i);
    String name = child.getNodeName();
    String contents = child.getTextContent();
}

Upvotes: 1

Related Questions