arcanum_jp’s blog

おっさんの日記

Jericho Html Parser

 OutputDocumentクラス

 使い方はいたって簡単だった。Sourceを生成したら、それを引数にOutputdocumentを生成して、独自に整形したいタグを見つけて、変更するだけ、

  OutputDocument doc = new OutputDocument(source);
  // TRタグを全部取得
  List tlist = doc.findAllElements();
  // 例えば1件目だけを変更するか・・・
  Element elem = (Element)tlist.get(0);

  // タグを自分で描画する必要があるらしい  
  String s = "<tr\n";
  Attributes ats = e.getAttributes();
  Iterator ite = ats.iterator();
  while(ite.hasNext()){
      Attribute at = (Attribute)ite.next();
      s = s + at.getKey() + "=\"" + at.getValue() + "\"\n";
  }
  s = s + ">";
  // これが無いと、タグの内部のタグが描画されない
  s = s + e.getContent();
  s = s + "</tr>";

  // 最後に特定のタグのみの内容を変更する。
  doc.replace(elem, s);

 でもこれだとこんな感じに描画されてしまう。使い方が間違っているのかな?

<table>
	<tr abc="hoge" def="fuga" class="foo">
		<td>111</td>
		<td>222</td>
		<td>333</td>
	</tr>
	<tr abc="hoge" def="fuga">
		<td>444</td>
		<td>555</td>
		<td>666</td>
	</tr>
</table>

   ↓↓↓

<table>
	<tr
abc="hoge"
def="fuga"
class="foo"
>
		<td>111</td>
		<td>222</td>
		<td>333</td>
	</tr>
	<tr abc="hoge" def="fuga">
		<td>444</td>
		<td>555</td>
		<td>666</td>
	</tr>
</table>

 結果から見ると、どうやらインデントとかの処理も自分で行う必要があるらしい