noshi’s diary

ゲームの事、映画やドラマ、思いついた事、プログラミングの事、雑記的なことを書いています

XMLを使ってみる

エクセルで入力した内容を使ってWEBページに反映できないかと思い、色々調べてみたところ、XMLを扱うことで、やりたいことを実現できそうだというところにいきつきました。 さて、肝心のXMLですが、XMLはその「データの意味」をコンピューターが判断できるようにしたファイルであるため、特定のデータのみ取り出したり、データを集計したりといったことが可能になるものです。 phpを使ってXMLを扱って、その内容を表示するにはphp-xmlというモジュールを組み込めば、すぐに使えるとのこと。 というのも、xmlを出力する際には、 PHP4の場合ですと、 $dom = domxml_open_file($xmlfilename); PHP5の場合ですと、 $dom = new DOMDocument; $rtn = $dom->load($xmlfilename); というDOM関数を使うからです。 ちなみに私はphp-xmlのインストール済みかどうかの確認もせずにプログラムを組んだところ、 Fatal error: Class ‘DOMDocument’ not foundと出ました。 DOMDocumentはありませんよっていうエラーが発生。当然ですが。。。 DOMDocumentはphp-xmlをインストールすれば、一緒に手に入るということなのでインストールしました。

yum install php-xml

Loaded plugins: downloadonly, fastestmirror, priorities, security Loading mirror speeds from cached hostfile * rpmforge: ftp.riken.jp base | 1.1 kB 00:00 extras | 2.1 kB 00:00 rpmforge | 1.9 kB 00:00 updates | 1.9 kB 00:00 119 packages excluded due to repository priority protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package php-xml.i386 0:5.1.6-39.el5_8 set to be updated --> Finished Dependency Resolution Dependencies Resolved

Transaction Summary

Install 1 Package(s) Upgrade 0 Package(s) Total download size: 99 k Is this ok [y/N]: y Downloading Packages: php-xml-5.1.6-39.el5_8.i386.rpm | 99 kB 00:00 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : php-xml 1/1 Installed: php-xml.i386 0:5.1.6-39.el5_8 Complete! インストール完了。 モジュール追加時は、httpdをリロードしておかないと動作しません。

service httpd reload

もしくは

/etc/rc.d/init'd/httpd reload

xmlを使う準備ができましたが、xmlファイルの作り方がわかりません。 ググると、エクセル2003からはファイル保存形式をxmlで保存可能とのこと。 エクセル2003? 持ってまてん。 自PCにはOOO(オープンオフィース)が入っているので、calcにxml形式で保存できるかもと思い、OOOを立ち上げ、保存形式のメニューを見たところ、Microsoft Excel 2003 xml(xml)という項目があるじゃありませんかぁ。 よし早速作成作成。 適当にデータを入力して、最後にxml形式を選択して保存しました。 出来上がったxmlは、こんな内容です。 <?xml version="1.0" encoding="UTF-8" ?> <?mso-application progid="Excel.Sheet"?> - <Workbook xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel"> - <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> - <Colors> - <Color> <Index>3</Index> <RGB>#000000</RGB> </Color> - <Color> <Index>4</Index> <RGB>#333333</RGB> </Color> - <Color> <Index>5</Index> <RGB>#c0c0c0</RGB> </Color> - <Color> <Index>6</Index> <RGB>#ff0000</RGB> </Color> </Colors> </OfficeDocumentSettings> - <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>9000</WindowHeight> <WindowWidth>13860</WindowWidth> <WindowTopX>240</WindowTopX> <WindowTopY>75</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> - <Styles> <Style ss:ID="Default" ss:Name="Default" /> - <Style ss:ID="Result" ss:Name="Result"> <Font ss:Bold="1" ss:Italic="1" ss:Underline="Single" /> </Style> - <Style ss:ID="Result2" ss:Name="Result2"> <Font ss:Bold="1" ss:Italic="1" ss:Underline="Single" /> <NumberFormat ss:Format="Currency" /> </Style> - <Style ss:ID="Heading" ss:Name="Heading"> ・ ・(以下ずらずらと長いコードが続く) ・ 【xmlファイルを展開するプログラム】 $data = array(); function add_person( $a, $b, $c ) { global $data; $data []= array( 'repair' => $a, 'price' => $b, 'sexual' => $c, ); } if ( $FILES['file']['tmp_name'] ) { $dom = DOMDocument::load( $FILES['file']['tmp_name'] ); $rows = $dom->getElementsByTagName( 'Row' ); $first_row = true; foreach ($rows as $row) { if ( !$first_row ) { $a = ""; $b = ""; $c = ""; $index = 1; $cells = $row->getElementsByTagName( 'Cell' ); foreach( $cells as $cell ) { $ind = $cell->getAttribute( 'Index' ); if ( $ind != null ) $index = $ind; if ( $index == 1 ) $a = $cell->nodeValue; if ( $index == 2 ) $b = $cell->nodeValue; if ( $index == 3 ) $c = $cell->nodeValue; $index += 1; } add_person( $a, $b, $c ); } $first_row = false; } } フォームを作成して、xmlファイルをアップロードしてみました。 <form enctype="multipart/form-data" action="test.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> <table width="600"> <tr> <td>Names file:</td> <td><input type="file" name="file" /></td> <td><input type="submit" value="Upload" /></td> </tr> </table> </form> ファイルアップロード後にアクセスされたページに、xmlファイル(元エクセル)のデータが表示されました。 長めの投稿でした。