gpt4 book ai didi

java - 如何检索元素混合子元素作为文本 (JDOM)

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:34 25 4
gpt4 key购买 nike

我有一个如下所示的 XML:

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

使用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() );

这给了我以下输出:

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.
'

我真正想要的是将元素的内容作为字符串获取,即 "This value must be <i>bigger</i> than the other." . getValue()接近但删除了 <i>标签。我想我想要类似 innerHTML 的东西对于 XML 文档...

我应该只对内容使用 XMLOutputter 吗?或者有更好的选择吗?

最佳答案

在 JDOM 伪代码中:

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

然而,作为Prashant Bhate wrote : content.getText() 给出直接文本,它只对带有文本内容的叶元素有用。

关于java - 如何检索元素混合子元素作为文本 (JDOM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com