如何将元素混合子元素检索为文本(JDOM)

st.*_*ver 5 java xml jdom

我有一个类似以下的XML:

<documentation>
    This value must be <i>bigger</i> than the other.
</documentation>
Run Code Online (Sandbox Code Playgroud)

使用JDOM,我可以得到以下文本结构:

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() );
Run Code Online (Sandbox Code Playgroud)

这给了我以下输出:

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.
'
Run Code Online (Sandbox Code Playgroud)

我真正想要的是将元素的内容作为字符串,即"This value must be <i>bigger</i> than the other.".getValue()接近但删除<i>标签.我想我想要像innerHTMLXML文档那样......

我应该在内容上使用XMLOutputter吗?还是有更好的选择吗?

Tho*_*mas -2

我想说你应该将你的文档更改为

<documentation>
  <![CDATA[This value must be <i>bigger</i> than the other.]]>
</documentation>
Run Code Online (Sandbox Code Playgroud)

为了遵守 XML 规范。否则<i>将被视为子元素<documentation>而不是内容。