DOMを利用してXMLファイルを読む その3

ちょいと本業が忙しくて...
ようやく落ち着いたので、XMLファイルのパースについてメモを書いておく。
明日書こうってのが1/4だったけど、ずいぶん忙しかったなぁ...


■ Documentオブジェクトを取得
XMLファイルをDocumentオブジェクトとして扱うために、DocumentBuilder型のインスタンスを生成し、そのparserメソッドを利用してXMLファイルのDocumentオブジェクトを取得する


document_builder_factory = DocumentBuilderFactory.newInstance();
document_builder = document_builder_factory.newDocumentBuilder();
document = document_builder.parse(
new BufferedInputStream(new FileInputStream(filename)));

■ 木構造で構成されているXMLファイルのrootを取得
XMLファイルは木構造で管理されているので、まずは根(root)を取得する


root_tree = document.getDocumentElement();

あとは、このroot_treeから木構造を辿りつつデータを解析していく感じ。