3

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?

2
  • Try to use java SAX for xml parsing Commented May 9, 2016 at 6:34
  • 1
    What do you mean by "doesn't work well"? Please explain your ultimate objective and provide more context so we can help you better. i.e. after you separate the XML what are you going to do with it? It is not at all clear that full-blown XML parsing is what you need here. Commented May 9, 2016 at 6:34

2 Answers 2

1
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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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]);
}

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.