How to Get Mixed Element Kids as Text (JDOM)

I have XML as shown below:

<documentation>
    This value must be <i>bigger</i> than the other.
</documentation>

Using JDOM, I can get the following text structures:

Document d = new SAXBuilder().build( new StringReader( s ) );
System.out.printf( "getText:          '%s'%n", d.getRootElement().getText() );
System.out.printf( "getTextNormalize: '%s'%n", d.getRootElement().getTextNormalize() );
System.out.printf( "getTextTrim:      '%s'%n", d.getRootElement().getTextTrim() );
System.out.printf( "getValue:         '%s'%n", d.getRootElement().getValue() );

which give me the following outputs:

getText:          '
    This value must be  than the other.
'
getTextNormalize: 'This value must be than the other.'
getTextTrim:      'This value must be  than the other.'
getValue:         '
    This value must be bigger than the other.
'

I really wanted to get the contents of the element as a string, namely "This value must be <i>bigger</i> than the other.". getValue()approaches, but removes the tag <i>. I guess I wanted something like innerHTMLfor XML documents ...

Should I just use XMLOutputter for the content? Or is there a better alternative?

+5
source share
3 answers

I would say that you should change your document to

<documentation>
  <![CDATA[This value must be <i>bigger</i> than the other.]]>
</documentation>

XML. <i> <documentation>, .

-1

JDOM:

for Object o in d.getRootElement().getContents()
   if o instanceOf Element
      print <o.getName>o.getText</o.getName>
   else // it a text
      print o.getText() 

, Prashant Bhate : content.getText() , .

0

HTML . , :

String snippet = new Source(html).getFirstElement().getContent().toString();

HTML , XML... .

-1

All Articles