我有一个如下所示的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吗?或者有没有更好的选择?
发布于 2011-04-29 22:18:51
我建议您应该将您的文档更改为
<documentation>
<![CDATA[This value must be <i>bigger</i> than the other.]]>
</documentation>以符合XML规范。否则,<i>将被视为<documentation>的子元素,而不是content。
发布于 2013-10-07 16:12:33
在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()给出了直接的文本,这只适用于包含文本内容的叶元素。
发布于 2011-04-29 22:18:14
Jericho HTML非常适合这类任务。你可以通过这样的代码块来完成你想要做的事情:
String snippet = new Source(html).getFirstElement().getContent().toString();一般来说,它对于处理超文本标记语言也是很好的,因为它不会试图强迫它成为XML...it处理它的更宽松的方式。
https://stackoverflow.com/questions/5833247
复制相似问题