コード帳 > Java > HTMLパーサ

JavaのSwingのHTMLパーサを使ってみた




結論

使いにくい。
タグ名とテキストは取得できるものの、属性値は取得できなかった。
属性値も取得できたわ
ただ一部のホワイトスペースが一部出てないけど、posっていう文書の位置から取得しろってことかな
重複する半角スペースが省略される仕様なのをやってるだけか、ただ一部出来てないとこが・・・
とにかく半角スペースの復元は無理そう
テキストデータの文字間の複数半角スペースも省略されてるからこれではどこが省略されてるか特定できない

コード

Hoge.java
  1. import javax.swing.text.html.parser.ParserDelegator;
  2. import javax.swing.text.html.HTMLEditorKit;
  3. import javax.swing.text.html.HTML;
  4. import javax.swing.text.MutableAttributeSet;
  5. import java.io.FileReader;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import javax.swing.text.BadLocationException;
  9. import java.util.Enumeration;
  10.  
  11. class Hoge extends HTMLEditorKit.ParserCallback
  12. {
  13. public static void main(String[] args) throws FileNotFoundException, IOException {
  14. Hoge hoge = new Hoge();
  15. FileReader file = new FileReader("unko.html");
  16.  
  17. pd.parse(file, hoge, true);
  18.  
  19. file.close();
  20.  
  21. }
  22.  
  23. Hoge() {
  24. }
  25.  
  26. public void flush() throws BadLocationException {
  27. }
  28.  
  29. public void handleText(char[] data, int pos) {
  30. System.out.printf("handleText(%d): '", pos);
  31. System.out.print(data);
  32. System.out.println("'");
  33. }
  34.  
  35. public void handleComment(char[] data, int pos) {
  36. System.out.printf("handleComment(%d): '", pos);
  37. System.out.print(data);
  38. System.out.println("'");
  39. }
  40.  
  41. public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  42. System.out.printf("handleStartTag(%d): ", pos);
  43. System.out.println(t);
  44. System.out.printf("Attributes: %d\n", a.getAttributeCount());
  45. for (Enumeration<?> e = a.getAttributeNames(); e.hasMoreElements();) {
  46. Object key = e.nextElement();
  47. System.out.print(key);
  48. System.out.print(" : ");
  49. System.out.println(a.getAttribute(key));
  50. System.out.println(key.getClass());
  51. System.out.println(a.getAttribute(key).getClass());
  52.  
  53. }
  54. }
  55.  
  56. public void handleEndTag(HTML.Tag t, int pos) {
  57. System.out.printf("handleEndTag(%d): ", pos);
  58. System.out.println(t);
  59. }
  60.  
  61. public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  62. System.out.printf("handleSimpleTag(%d): ", pos);
  63. System.out.println(t);
  64. System.out.printf("Attributes: %d\n", a.getAttributeCount());
  65. for (Enumeration<?> e = a.getAttributeNames(); e.hasMoreElements();) {
  66. Object key = e.nextElement();
  67. System.out.print(key);
  68. System.out.print(" : ");
  69. System.out.println(a.getAttribute(key));
  70. System.out.println(key.getClass());
  71. System.out.println(a.getAttribute(key).getClass());
  72. }
  73. }
  74.  
  75. public void handleError(String errorMsg, int pos) {
  76. System.out.printf("handleError(%d): ", pos);
  77. System.out.println(errorMsg);
  78. }
  79.  
  80. public void handleEndOfLineString(String eol) {
  81. System.out.print("handleEndOfLineString: ");
  82. System.out.println(eol);
  83. }
  84. }

テストデータ

unko.html
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <!--[if IE 7]>
  3. <linkrel="stylesheet" type="text/css" href="/stylesheets/ie.css" />
  4. <![endif]-->
  5. <META http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
  6. </HEAD>
  7. <BODY bgcolor="red">
  8. <hoge > ほげ~ </hoge>
  9.  
  10. <unko value="123"> AIUEO <pre> hoge </pre> umi </unko> aoi <br /> sor aa <br>
  11. <p >MANKO</p>
  12. <!-- ぶりぶりぶー -->
  13. <chinko href="unko.html" />
  14.       
  15.  
  16. </BODY>
  17. </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:
 

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2013年10月26日 23:58