Search code examples
javaxmljdom-2

How to get inner text from children XML tags using jdom2?


My XML file is structured like so:

<parent xml:space="preserve">
Hello, my name is
    <variable type="firstname">ABC</variable>
and my last name is 
    <variable type="lastname">XYZ</variable>
</parent>

I need a way to get the text output in this format:

"Hello, my name is ABC and my last name is XYZ".

Now the issue with using jdom2 is that element.getText() method returns the entire string as a single string (with no regard to position of the child tags):

"Hello, my name is and my last name is".

Is there anyway I can get the position of the child tags/delimit them, so that even a manual variable insert can be done at some later point?


Solution

  • getText is specified in JDOM to return the immediate Text content of the element. JDOM also has the method getValue() which returns:

    Returns the XPath 1.0 string value of this element, which is the complete, ordered content of all text node descendants of this element (i.e. the text that's left after all references are resolved and all other markup is stripped out.)

    Applying this to your document:

        Document doc = new SAXBuilder().build("parentwtext.xml");
        Element root = doc.getRootElement();
        System.out.println(root.getValue());
    

    I get the output (there's an empty line at the beginning I can't show in here):

    Hello, my name is
        ABC
    and my last name is 
        XYZ