XMLのパースがおそーい><

これだけのHTMLをパースするのに10秒かかる。jdomとdom4jを試したがどちらも同じ。

ということは俺がオカシイ事をしているはずなのだ。恐らくdtdをダウンロードとかしちゃってるんじゃないかなぁ、毎回。そりゃ時間かかるだろうけど…。

なんとかキャッシュするなり、DTD無視(無視してパースできるんだっけ?)するなりしてくれないかなぁ、とか。

ソースのHTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>foobarbaz</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  </head>
  <body>

<p>foo<span>bar</span>baz</p>

  </body>
</html>

jdom版

import java.io.File;
import java.util.Date;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;

public class MainJdom {
	
	public static void main(String[] args) throws Exception {
		new MainJdom().run();
	}

	private void run() throws Exception {
		File html = new File("test.html");
		SAXBuilder sb = new SAXBuilder();
		sb.setValidation(false);
		System.out.println(new Date());
		Document document = sb.build(html);
		System.out.println(new Date());
		System.out.println(document);
	}
}
    <dependency>
      <groupId>jdom</groupId>
      <artifactId>jdom</artifactId>
      <version>1.0</version>
    </dependency>

dom4j

import java.io.File;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class MainDom4j {
	
	public static void main(String[] args) throws Exception {
		new MainDom4j().run();
	}

	private void run() throws Exception {
		File html = new File("test.html");
		SAXReader sr = new SAXReader();
		sr.setValidation(false);
		System.out.println(new Date());
		Document document = sr.read(html);
		System.out.println(new Date());
		
		System.out.println(document);
	}
}
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>

追記

Xercesを使ってみた。(これはXercesを使う、って表現でいいのかな?) やっぱし10秒。

Xerces版

import java.util.Date;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;

public class MainXerces {
	
	public static void main(String[] args) throws Exception {
		new MainXerces().run();
	}

	private void run() throws Exception {
		String html = "test.html";
		DOMParser parser = new DOMParser();
//		parser.setValidation(false);
		System.out.println(new Date());
		parser.parse(html);
		Document document = parser.getDocument();
		System.out.println(new Date());
		
		System.out.println(document);
	}
}

しかも結果がnullかよ…。DOMParser#parse(String)の使い方間違ってる?w

Sat Mar 01 16:39:16 JST 2008
Sat Mar 01 16:39:26 JST 2008
[#document: null]