JavaのSwingのHTMLパーサを使ってみた
結論
使いにくい。
タグ名とテキストは取得できるものの、属性値は取得できなかった。
属性値も取得できたわ
ただ一部のホワイトスペースが一部出てないけど、posっていう文書の位置から取得しろってことかな
重複する半角スペースが省略される仕様なのをやってるだけか、ただ一部出来てないとこが・・・
とにかく半角スペースの復元は無理そう
テキストデータの文字間の複数半角スペースも省略されてるからこれではどこが省略されてるか特定できない
コード
Hoge.java
import javax.swing.text.html.parser.ParserDelegator;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.MutableAttributeSet;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.text.BadLocationException;
import java.util.Enumeration;
{
Hoge hoge = new Hoge();
pd.parse(file, hoge, true);
file.close();
}
Hoge() {
}
}
public void handleText(char[] data, int pos) {
System.
out.
printf("handleText(%d): '", pos
); }
public void handleComment(char[] data, int pos) {
System.
out.
printf("handleComment(%d): '", pos
); }
System.
out.
printf("handleStartTag(%d): ", pos
); System.
out.
printf("Attributes: %d\n", a.
getAttributeCount()); for (Enumeration<?> e = a.getAttributeNames(); e.hasMoreElements();) {
System.
out.
println(a.
getAttribute(key
)); System.
out.
println(key.
getClass()); System.
out.
println(a.
getAttribute(key
).
getClass());
}
}
public void handleEndTag
(HTML.Tag t,
int pos
) { System.
out.
printf("handleEndTag(%d): ", pos
); }
System.
out.
printf("handleSimpleTag(%d): ", pos
); System.
out.
printf("Attributes: %d\n", a.
getAttributeCount()); for (Enumeration<?> e = a.getAttributeNames(); e.hasMoreElements();) {
System.
out.
println(a.
getAttribute(key
)); System.
out.
println(key.
getClass()); System.
out.
println(a.
getAttribute(key
).
getClass()); }
}
public void handleError
(String errorMsg,
int pos
) { System.
out.
printf("handleError(%d): ", pos
); }
public void handleEndOfLineString
(String eol
) { System.
out.
print("handleEndOfLineString: "); }
}
テストデータ
unko.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!--[if IE 7]>
<linkrel="stylesheet" type="text/css" href="/stylesheets/ie.css" />
<![endif]-->
<META http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <hoge > ほげ~ </hoge>
<unko value="123"> AIUEO
<pre> hoge
</pre> umi
</unko> aoi
<br /> sor aa
<br> <!-- ぶりぶりぶー -->
<chinko href="unko.html" />
出力結果
handleStartTag(91): html
Attributes: 0
handleStartTag(98): head
Attributes: 0
handleComment(108): '[if IE 7]>
<linkrel="stylesheet" type="text/css" href="/stylesheets/ie.css" />
<![endif]'
handleSimpleTag(212): meta
Attributes: 2
content : text/html; charset=Shift_JIS
class javax.swing.text.html.HTML$Attribute
class java.lang.String
http-equiv : Content-Type
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleStartTag(284): title
Attributes: 0
handleEndTag(291): title
handleEndTag(300): head
handleStartTag(308): body
Attributes: 1
bgcolor : red
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleError(338): tag.unrecognized hoge ? ?
handleSimpleTag(332): hoge
Attributes: 0
handleError(357): end.unrecognized hoge ? ?
handleText(343): 'ほげ~ '
handleSimpleTag(349): hoge
Attributes: 1
endtag : true
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleError(364): tag.unrecognized unko ? ?
handleError(376): invalid.tagatt value unko ?
handleText(358): ' '
handleSimpleTag(358): unko
Attributes: 1
value : 123
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleText(378): ' AIUEO'
handleStartTag(387): pre
Attributes: 0
handleText(392): ' hoge '
handleEndTag(401): pre
handleError(421): end.unrecognized unko ? ?
handleText(408): 'umi '
handleSimpleTag(413): unko
Attributes: 1
endtag : true
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleText(421): ' aoi'
handleSimpleTag(426): br
Attributes: 0
handleText(433): 'sor aa'
handleSimpleTag(442): br
Attributes: 0
handleStartTag(447): p
Attributes: 0
handleText(453): 'MANKO'
handleEndTag(458): p
handleComment(463): ' ぶりぶりぶー '
handleError(495): tag.unrecognized chinko ? ?
handleError(512): invalid.tagatt href chinko ?
handleSimpleTag(487): chinko
Attributes: 1
href : unko.html
class javax.swing.text.html.HTML$Attribute
class java.lang.String
handleText(515): ' '
handleEndTag(528): body
handleEndTag(536): html
handleEndOfLineString:
最終更新:2013年10月26日 23:58