5

私は次のような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>タグを削除します。innerHTMLXMLドキュメントのようなものが欲しかったと思います...

コンテンツにXMLOutputterを使用する必要がありますか?または、より良い代替手段はありますか?

4

3 に答える 3

0

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が書いたように:content.getText()は、テキストコンテンツを含むリーフ要素でのみ適切に役立つ即時テキストを提供します。

于 2013-10-07T08:12:33.423 に答える
-1

Jericho HTMLは、この種のタスクに最適です。次のようなコードブロックを使用して、実行しようとしていることを正確に実行できます。

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

また、HTMLを強制的にXMLにしようとしないため、一般的なHTMLの操作にも最適です...HTMLをはるかに寛大に処理します。

于 2011-04-29T14:18:13.573 に答える
-2

ドキュメントを次のように変更する必要があります

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

XML仕様に準拠するため。それ以外の場合は、コンテンツではなく<i>の子要素と見なされ<documentation>ます。

于 2011-04-29T14:18:51.307 に答える