{"id":219,"date":"2015-06-09T23:14:55","date_gmt":"2015-06-09T14:14:55","guid":{"rendered":"http:\/\/www.cl.cs.okayama-u.ac.jp\/?p=219"},"modified":"2017-03-28T23:18:14","modified_gmt":"2017-03-28T14:18:14","slug":"scala-html-dom","status":"publish","type":"post","link":"https:\/\/www.cl.cs.okayama-u.ac.jp\/?p=219","title":{"rendered":"Scala\u3067html\u3092\u30d1\u30fc\u30b9 (DOM) (2015\/6\/9)"},"content":{"rendered":"<p>Scala\u3067html\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308d\u3044\u308d\u51e6\u7406\u3092\u3057\u305f\u3044\u3068\u304d\u304c\u3042\u308a\u307e\u3059\uff0eDOM\u306e\u69cb\u9020\u306b\u843d\u3068\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u306e\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f(2015.6.9) Scala\u306eversion\u306f 2.10.4\uff0eEclipse4.4\u4e0a\u3067 Scala\u306f plugin\u3067\u5165\u308c\u307e\u3057\u305f\uff0e<\/p>\n<h3>\u307e\u305a\u6e96\u5099<\/h3>\n<p>nu.validator.htmlparser.dom.HtmlDocumentBuilder\u3092\u4f7f\u3046\u305f\u3081\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff0egoogle\u3067\u691c\u7d22\u3057\u3066  htmlparser-1.4.zip \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092download\u3057\u307e\u3059\uff0e<\/p>\n<p>\u6b21\u306b\u5c55\u958b\u3057\u3066 htmlparser-1.4.jar \u30d5\u30a1\u30a4\u30eb\u3092Eclipse\u306e Scala\u306b\u8aad\u307f\u8fbc\u307e\u305b\u307e\u3059\uff0e\u3084\u308a\u65b9\u306fEclipse\u306e [\u30d7\u30ed\u30b8\u30a7\u30af\u30c8]&gt;[\u30d7\u30ed\u30d1\u30c6\u30a3]&gt;[Java\u306e\u30d3\u30eb\u30c9\u30fb\u30d1\u30b9]\u306e[\u30e9\u30a4\u30d6\u30e9\u30ea\u30fc]\u30bf\u30d6\u3092\u958b\u304d\uff0c\u300c\u5916\u90e8jar\u8ffd\u52a0\u300d\u3067\u3055\u304d\u307b\u3069\u306e htmlparser-1.4.jar\u3092\u8aad\u307f\u8fbc\u307e\u305b\u307e\u3059\uff0e\u3053\u308c\u3067nu.validator.htmlparser.dom.HtmlDocumentBuilder\u3092import\u51fa\u6765\u307e\u3059\uff0e<\/p>\n<h3>\u30d7\u30ed\u30b0\u30e9\u30e0<\/h3>\n<pre>import scala.io.Source\r\n\r\nimport nu.validator.htmlparser.dom.HtmlDocumentBuilder\r\nimport java.io.StringReader\r\nimport org.xml.sax.InputSource\r\n\r\nobject Domp {\r\n def main(args: Array[String]) {\r\n   val builder = new HtmlDocumentBuilder()\r\n   val source = Source.fromFile(\"\/home\/\u306a\u306b\u304bhtml\u306e\u30d5\u30a1\u30a4\u30eb\")\r\n   val sreader = new StringReader(source.mkString)\r\n   \r\n   \/\/ parse\u3092\u3057\u307e\u3059\r\n   val dom = builder.parse(new InputSource(sreader))\r\n   \r\n   \/\/ \u4e00\u756a\u4e0a\u306e\u4e00\u3064\u4e0b\u306e\u5b50\u4f9b\u306fhtml\uff0e\u306a\u306e\u3067\u305d\u306e\u4e0b\u3092\u53d6\u3063\u3066\u307f\u307e\u3059\r\n   val child = dom.getChildNodes().item(0) \/\/\u3053\u306e0\u756a\u76ee\u306f \u30c8\u30c3\u30d7\u306ehtml\r\n   val size = child.getChildNodes().getLength \/\/html\u306e\u4e0b\u3092\u53d6\u308b\r\n   \/\/\u3069\u3046\u3082Java\u30d9\u30fc\u30b9\u3089\u3057\u304f\u914d\u5217\u306b\u5bfe\u3057\u3066\u6570\u3092\u6570\u3048\u3066\u30eb\u30fc\u30d7\u3092\u56de\u3055\u306a\u3044\u3068\u3068\u308c\u306a\u3044!?\r\n   for(i &lt;- 0 until size){\r\n         println(\"i=\"+i) \/\/\u4f55\u756a\u76ee\u306e\u5b50\u4f9b\u304b\u8868\u793a\u3055\u305b\u3066\u307f\u307e\u3057\u305f\r\n        println (child.getChildNodes().item(i).getNodeName) \/\/\u5b50\u306e\u30bf\u30b0\u540d\r\n   }\r\n }\r\n}\r\n<\/pre>\n<pre>\u5165\u529b\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u3053\u3093\u306a\u611f\u3058<br \/>test.html<br \/><br \/>&lt;html&gt;<br \/> &lt;title&gt;\u30c6\u30b9\u30c8\u30bf\u30a4\u30c8\u30eb&lt;\/title&gt;<br \/> &lt;body&gt;<br \/> &lt;h1&gt; \u30c6\u30b9\u30c8 &lt;\/h1&gt;<br \/> &lt;\/body&gt;<br \/>&lt;\/html&gt;<br \/><\/pre>\n<p>\u3059\u308b\u3068\u51fa\u529b\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059<\/p>\n<pre>i=0<br \/>head<br \/>i=1<br \/>body<br \/><\/pre>\n<p>\u3064\u307e\u308a html\u306e\u30bf\u30b0\u306e\u4e0b\u306b\u306f title\u3068body\u30bf\u30b0\u304c\u5b50\u30ce\u30fc\u30c9\u3057\u3066\u3042\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff0e<\/p>\n<p>child\u306a\u3069\u306e\u578b\u306f org.w3c.dom.Node\u306a\u306e\u3067\u3042\u3068\u306f\u8abf\u3079\u308b\u3068\u3044\u308d\u3044\u308d\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u305d\u3046\u3067\u3059\uff0e<\/p>\n<p>\u53c2\u8003: <a class=\"external-link\" href=\"http:\/\/blog.mwsoft.jp\/article\/45131631.html\">http:\/\/blog.mwsoft.jp\/article\/45131631.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scala\u3067html\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308d\u3044\u308d\u51e6\u7406\u3092\u3057\u305f\u3044\u3068\u304d\u304c\u3042\u308a\u307e\u3059\uff0eDOM\u306e\u69cb\u9020\u306b\u843d\u3068\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u306e\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f(2015.6.9) Scala\u306eversion\u306f 2.10.4\uff0eEclipse4.4\u4e0a\u3067 S &hellip; <a href=\"https:\/\/www.cl.cs.okayama-u.ac.jp\/?p=219\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219"}],"version-history":[{"count":3,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions\/222"}],"wp:attachment":[{"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cl.cs.okayama-u.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}