XHTMLの妥当性検証をしたいときがあります。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>タイトル</title>
<meta name="description" content="説明" />
<meta name="keywords" content="説明" />
<meta name="author" content="名前" />
<meta name="copyright" content="コピーライト" />
<link rel="stylesheet" type="text/css" media="screen, projection, tv" href="hogehoge" />
<link rel="stylesheet" type="text/css" media="print" href="hogehoge" />
<link rel="alternate stylesheet" type="text/css" media="screen, projection, tv" href="hogehoge" title="hogehoge" />
<script type="text/jacascript" src="hogehoge"></script>
<link rel="shortcut Icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
<link rel="contens" href="/" title="目次" />
<link rel="help" href="/about/" title="ヘルプ" />
<link rel="index" href="/sitemap" title="サイトマップ" />
</head>
<body>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>タイトル</title>
<meta name="description" content="説明" />
<meta name="keywords" content="説明" />
<meta name="author" content="名前" />
<meta name="copyright" content="コピーライト" />
<link rel="stylesheet" type="text/css" media="screen, projection, tv" href="hogehoge" />
<link rel="stylesheet" type="text/css" media="print" href="hogehoge" />
<link rel="alternate stylesheet" type="text/css" media="screen, projection, tv" href="hogehoge" title="hogehoge" />
<script type="text/jacascript" src="hogehoge"></script>
<link rel="shortcut Icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
<link rel="contens" href="/" title="目次" />
<link rel="help" href="/about/" title="ヘルプ" />
<link rel="index" href="/sitemap" title="サイトマップ" />
</head>
<body>
</body>
</html>
こういうファイルがあるときに、妥当性検証をJavaでするには、JAXPなり、Xercesを使えば、すぐにできます。
package org.sssg.soft.sample.jaxp; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXParseException; public class CheckXml { public void run(File file) { try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); factory.setAttribute( "http://apache.org/xml/features/validation/schema", true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException e) { System.out.println("warning: " + e.getLineNumber()); System.out.println(e.getMessage()); } public void error(SAXParseException e) { System.out.println("error: " + e.getLineNumber()); System.out.println(e.getMessage()); } public void fatalError(SAXParseException e) { System.out.println("fatalError: " + e.getLineNumber()); System.out.println(e.getMessage()); } }); Document doc = builder.parse(file); doc.getClass(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { CheckXml app = new CheckXml(); app.run(new File(args[0])); } }
次のようなデータでは、TITLEがxhtmlの要素として適切ではないので、エラーがでます。(かなり実行に時間がかかりますが…。XML Schemeなどでデータ定義をして、そのファイルをクラスパスでみえるところにおけば、実行にはそれほど時間がかからないはずです。)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <TITLE>タイトル</TITLE> </head> <body> </body> </html>