Search code examples
javaxmldomdomparser

Tokenizing XML output using Java DOM parser - multiple child node values


in my expressions tab in Eclipse, I am able to see that the following output

\nmilk product, cultured\ncow milk\ncheese farm\n

by calling

eElement.getElementsByTagName("XMLTAG").item(0).getTextContent());

How to tokenize the string using \n using indexof such that I can have separate string values ie:

String dairy1 = "milk product, cultured";
String dairy2 = "cow milk"
String dairy3 = "cheese farm"

Solution

  • The most convenient way should be

    String[] toks =
    eElement.getElementsByTagName("XMLTAG").item(0).getTextContent().split( "\n" );
    

    The empty element [0] must be skipped if the \n is always at the beginning.