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

Javaの流儀はまったくわからんけれども、とりあえず書いてみた。
XMLファイルのパース部分の動きについてのメモは明日書こう。
今日はその前準備メモ。


■ パースしたXML形式データの扱い

読み込んでパースしたXML形式のデータ構造は、そのまま出力するのではなく、独自定義したクラスに格納することにした。
そのままSystem.out.printlnで出力して終了ってのは普通はないよね。

1つのクラスインスタンスで、1つのチャンネルのデータ(タイトルとURL)を格納するので、配列として利用する。

■ 独自定義したクラスを配列で利用する

独自に定義したChannelクラスを配列として使うのには、要素毎にnewで生成したインスタンスを代入してやる必要がある。
そうしないと、NullPointerExceptionが...
初期化だけなら、こんな感じ。

Channel[] channels;
channels = new Channel[3];
for(int i = 0; i < 3; i++) {
    channels[i] = new Channel();
}

C言語的に考えると、まぁ当然といえば当然だんだけど...どうなんだろうなぁ...


■ とりあえずテストコードを書いてみた TestXmlReader.java

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;



public class TestXmlReader {
    public static void main(String[] args) {
        DocumentBuilderFactory document_builder_factory;
        DocumentBuilder document_builder;
        Document document;
        Element root_tree, sub_tree;
        NodeList channel_node_list, title_node_list, url_node_list;
        Node title_node, url_node;

        String filename;
        int ch_idx, ch_max;

        Channel[] channels;

        filename = "test.xml";

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

            root_tree = document.getDocumentElement();

            channel_node_list = root_tree.getElementsByTagName("channel");
            ch_max = channel_node_list.getLength();
            channels = new Channel[ch_max];
            
            for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
                sub_tree = (Element)channel_node_list.item(ch_idx);
                title_node_list = sub_tree.getElementsByTagName("title");
                url_node_list = sub_tree.getElementsByTagName("url");
                title_node = title_node_list.item(0);
                url_node = url_node_list.item(0);
                channels[ch_idx] = new Channel();
                channels[ch_idx].set_title(
                    title_node.getFirstChild().getNodeValue());
                channels[ch_idx].set_url(
                    url_node.getFirstChild().getNodeValue());

                System.out.println(new Integer(ch_idx).toString());
                System.out.println(channels[ch_idx].get_title());
                System.out.println(channels[ch_idx].get_url());
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }


class Channel {
    private String title;
    private String url;

    public Channel() {
        this.title = null;
        this.url   = null;
    }
    public void set_title(String buff) {
        this.title = buff;
    }
    public void set_url(String buff) {
        this.url = buff;
    }
    public String get_title() {
        return this.title;
    }
    public String get_url() {
        return this.url;
    }
}