Search code examples
javaxmljdom

JDOM get string of containing content


lets say I have some xml:

<document>blabla<bold>test<list><item>hello<italics>dfh</italics></item></list></bold>sdfsd</document>

and I now need to get the content of as a string, so I would have

blabla<bold>test<list><item>hello<italics>dfh</italics></item></list></bold>sdfsd

i have been messing with this in my head for a while now, and I haven't seem to be able to figure it out.

Hope to get some directions to what I have to do.

EDIT:

just to be clear, lets say I have the XML like this:

SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new StringReader("<document>blabla<bold>test<list><item>hello<italics>dfh</italics></item></list></bold>sdfsd</document>"));

and I now need to get the content of


Solution

  • It is very unusual to need to get an inconsistent subset of an XML document like you want. It's much more common to get just the text content: blabla test hello dfh sdfsd

    Note that you can get a subset of the content as the "contentlist" of the root element, and then output just that list as a string:

        XMLOutputter xout = new XMLOutputter();
        String txt = xout.outputString(doc.getRootElement().getContent());
        System.out.println(txt);
    

    For me, I wrote the code:

    public static void main(String[] args) throws JDOMException, IOException {
        SAXBuilder sb = new SAXBuilder();
        Document doc = sb.build(new StringReader("<document>blabla<bold>test<list><item>hello<italics>dfh</italics></item></list></bold>sdfsd</document>"));
    
        XMLOutputter xout = new XMLOutputter();
        String txt = xout.outputString(doc.getRootElement().getContent());
        System.out.println(txt);        
    }
    

    and it output:

    blabla<bold>test<list><item>hello<italics>dfh</italics></item></list></bold>sdfsd