PHP で、XML ファイルを読み込み、表示する。

Google Maps API のデータをサーバ側で XML として扱うことを考えてみる。まずは以下のような XML ファイルを用意する。

<?xml version="1.0" encoding="UTF-8"?>
<marker
    lng="-121.9485855102539" lat="37.318707484418496" <font color="blue">← ここは、</font><b><font color="orange">経度</font><font color="blue">・</font><font color="red">緯度</font></b><font color="blue">の順で入る</font>
    icon="/yas/images/icon_mexican.png" <font color="blue">← アイコン画像へのルートからのパス + ファイル名 (https:// は、JavaScript 中の最初に記述済み)</font>
    name="Chevys Winchester" <font color="blue">← 吹き出し中の一番上に表示される、マーカーの名前</font>
    link="https://www.chevys.com/" <font color="blue">← マーカーの名前をクリックすると表示されるウェブページ</font>
    address1="550 S. Winchester" <font color="blue">← マーカーの住所</font>
    address2=" San Jose, CA 95128" <font color="blue">← マーカーの住所</font>
    phone="(408) 241-0158" <font color="blue">← 電話番号</font>
/>

このデータを読み込んでブラウザで表示 (または JavaScript で処理) するには、PHP では以下のようになる。

<?php // 変数の初期化 $XML_HEADER = '<?xml version="1.0" encoding="UTF-8"?>'; $HOME_DIR = '/home/yourname'; // XMLファイル $XML_FILE = '/www/perltips/gmap.xml'; $output = ''; // 出力バッファ $text = ''; // テキストバッファ init(); //テキストノードの値 function characters($parser, $text) { <pre>// for example convert from UTF-8 to EUC-JP // $text = mb_convert_encoding($text, "EUC-JP", "UTF-8"); } //要素の開始 function startElement($parser, $name, $attr) {
global $output;

if($name == 'marker') {

    // 属性の取得
    $lng      = $attr['lng'   ];
    $lat      = $attr['lat'   ];
    $icon     = $attr['icon'  ];
    $name     = $attr['name'  ];
    $link     = $attr['link'  ];
    $address1 = $attr['address1'];
    $address2 = $attr['address2'];
    $phone    = $attr['phone' ];
    $city     = $attr['comment'  ];

    $output    .=    "\t<marker"
            .        "\t\tlng      = \"$lng\" lat = \"$lat\""
            .        "\t\ticon     = \"$ICON_DEFAULT\""
            .        "\t\tname     = \"$name\""
            .        "\t\tlink     = \"$link\""
            .        "\t\taddress1 = \"$address1\""
            .        "\t\taddress2 = \"$address2\""
            .        "\t\tphone    = \"$phone\""
            .        "\t\tcomment  = \"$comment\""
            .    "\\t/>";
}
} //要素の終了 function endElement($parser, $name) {
global $output;

if($name == 'markers') {

    // XML データを作成
    $output    = $XML_HEADER
            .    "<markers>\
"
            .    $output
            .    "</markers>\
";
    header('Content-type: text/xml');
    print $output;
}
} function init() {
global $HOME_DIR, $XML_FILE;

// XMLパーサー作成 ("UTF-8")
$xml_parser = xml_parser_create('UTF-8');

// 大文字変換を行わない
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);

// start および end 要素のハンドラを設定する
xml_set_element_handler($xml_parser, 'startElement', 'endElement');

// 文字データハンドラを設定する
xml_set_character_data_handler($xml_parser, 'characters');

$fp = fopen("$HOME_DIR$XML_FILE", 'r')
or die("cannot open an XML file:  $HOME_DIR$XML_FILE");

while ($data = fread($fp, 4096)) {

    // XMLパース処理
    if (!xml_parse($xml_parser, $data, feof($fp))) {

    // パースエラー処理
    die(sprintf("XML error: %s at line %d",
        xml_error_string(xml_get_error_code($xml_parser)),
        xml_get_current_line_number($xml_parser)));
    }
}

// XMLパーサの開放
xml_parser_free($xml_parser);
} ?>

mozilla.org の AJAX: Getting Started にあるように、PHP から吐き出す XML データをJavaScript で扱う場合は以下の header('Content-type: text/xml'); 行が重要である。

<?php //要素の終了 function endElement($parser, $name) { ... <pre> header('Content-type: text/xml'); print $output; ... } ?>