Google spreadsheet にデータを保存しておいて、Zendでアクセスして条件に合ったデータのみ、xmlファイルで表示させています( http://withmama.lolipop.jp/kaihatsu/hatena_1.php )。このファイルをダウンロードさせるために上記のページを生成したプログラムを訂正して、下のようなプログラムを作りました。実際に動かしてみる( http://withmama.lolipop.jp/kaihatsu/hatena.html )と、プログラム中の「$multimedia->addAttribute('format', 'nav-u:sound" href="file://./nav_u_common/2" repeat="1');」の「"」の部分がダウンロードしたファイルでは「"」に自動で置換されてしまいます。
●ブラウザーで表示したxmlファイルの該当部分
<multimedia format="nav-u:sound" href="file://./nav_u_common/2" repeat="1">
●ダウンロードしたxmlファイルの該当部分
<multimedia format="nav-u:sound" href="file://./nav_u_common/2" repeat="1">
解決方法をご存知の方がいらっしゃればありがたいのですが・・・。
☆ダウンロードするプログラムは下にあります。
☆xmlファイルをダウンロードさせるプログラム
$ne_lat = '36';
$ne_lng = '140';
$sw_lat = '35.8';
$sw_lng = '139.6';
$xml = simplexml_load_string(''.'<glorie version="1.0" model="Sony corporation:nav-u:v1.00"></glorie>');
/*
http://www.ibm.com/developerworks/jp/xml/library/x-googledata1/i... の中の
「ワークシートのデータを配列として取得する」(リスト 9)参照
*/
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
// set credentials for ClientLogin authentication
$user = "****";
$pass = "****";
try {
// connect to API
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Spreadsheets($client);
// get spreadsheet entry
$ssEntry = $service->getSpreadsheetEntry('https://spreadsheets.google.com/feeds/spreadsheets/0Aojk_zIECowM...);
/*
get worksheets in this spreadsheet ワークシートが複数だと別の処理が必要か? ワークシートが1つなので動いている?
*/
$wsFeed = $ssEntry->getWorksheets();
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
foreach($wsFeed as $wsEntry){
$rows = $wsEntry->getContentsAsRows();
/*
echo "
";
print_r($rows);
echo "";
*/
foreach ($rows as $data) {
if(($data['lat'] > $ne_lat or $data['lat'] < $sw_lat) or ($data['lon'] < $sw_lng or $data['lon'] > $ne_lng)){
continue;
}
/*
要素に子要素を追加 参照 http://webcache.googleusercontent.com/search?q=cache:roa-1yVHYQU...
*/
$format = $xml->addChild('format');
$datum = $format->addChild('datum','tokyo');
$unit = $format->addChild('unit','degree');
$time = $format->addChild('time','2009-05-05T12:00:00+09:00');
$gpoi = $xml->addChild('gpoi');
$poi = $gpoi->addChild('poi');
$point = $poi->addChild('point');
$pos = $point->addChild('pos');
//更に子要素を追加
$pos->addChild('lat', _h($data['lat']));
$pos->addChild('lon', _h($data['lon']));
$name = $poi->addChild('name');
//更に子要素を追加
$name->addChild('nb', _h($data['nb']));
$name->addChild('rt', _h($data['rt']));
$address = $gpoi->addChild('address');
$address->addChild('address-text', _h($data['address-text']));
$category = $gpoi->addChild('category', _h($data['category']));
$category->addAttribute('vendor','Sony:nav-u');
$linkfile = $gpoi->addChild('linkfile');
$image = $linkfile->addChild('image');
$image->addAttribute('href','');
$image ->addChild('comment', _h($data['comment']));
$multimedia = $linkfile->addChild('multimedia');
$multimedia->addChild('comment', _h($data['comment_2']));
${'activate-condition'} = $multimedia->addChild('activate-condition');
${'activate-radius'} = ${'activate-condition'}->addChild('activate-radius');
${'activate-radius'}->addChild('outmost', _h($data['outmost']));
${'activate-radius'}->addChild('inmost', _h($data['inmost']));
$multimedia->addAttribute('format', 'nav-u:sound" href="file://./nav_u_common/2" repeat="1');
}
}
$xml->asXML('test.xml');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.xml"');
header('Content-Length: '.filesize('test.xml'));
readfile('test.xml');
/*
*エスケープ処理
* XMLの値の中に&が入ってるとエラーで落ちるので
*
* @param String
* @return String
*/
function _h($value)
{
return htmlspecialchars($value, ENT_QUOTES);
}
?>
解決して良かったです。
それでは回答させていただきます。
$multimedia->addAttribute('format', 'nav-u:sound" href="file://./nav_u_common/2" repeat="1');
上記を次のように修正するといいのではないですか?
$multimedia->addAttribute('format', 'nav-u:sound'); $multimedia->addAttribute('href', 'file://./nav_u_common/2'); $multimedia->addAttribute('repeat', '1');
simplexml_load_string()で返されるSimpleXMLElementオブジェクトに、addChild()やaddAttribute()で子要素を追加したり、追加した要素に属性を設定する処理の部分ですので、SimpleXMLElementクラスのマニュアルを参考にするといいです。
http://www.php.net/manual/ja/class.simplexmlelement.php
addAttribute()は属性の名前と値をセットで指定して追加します。
・SimpleXMLElement::addAttribute
http://www.php.net/manual/ja/simplexmlelement.addattribute.php
パラメータ
name
追加する属性の名前。
value
属性の値。
属性とは、HTMLでもおなじみだと思いますが、img要素なら「src」、「alt」、「width」、「height」等が該当します。
SONYのカーナビ用のポイントファイルデータの仕様は分かりませんが、<multimedia format="nav-u:sound" href="file://./nav_u_common/2" repeat="1">は、multimedia要素で「format」、「href」、「repeat」が属性と考えられますので、コメントしたようにaddAttribute()で一つずつ属性を追加します。
解決して良かったです。
それでは回答させていただきます。
$multimedia->addAttribute('format', 'nav-u:sound" href="file://./nav_u_common/2" repeat="1');
上記を次のように修正するといいのではないですか?
$multimedia->addAttribute('format', 'nav-u:sound'); $multimedia->addAttribute('href', 'file://./nav_u_common/2'); $multimedia->addAttribute('repeat', '1');
simplexml_load_string()で返されるSimpleXMLElementオブジェクトに、addChild()やaddAttribute()で子要素を追加したり、追加した要素に属性を設定する処理の部分ですので、SimpleXMLElementクラスのマニュアルを参考にするといいです。
http://www.php.net/manual/ja/class.simplexmlelement.php
addAttribute()は属性の名前と値をセットで指定して追加します。
・SimpleXMLElement::addAttribute
http://www.php.net/manual/ja/simplexmlelement.addattribute.php
パラメータ
name
追加する属性の名前。
value
属性の値。
属性とは、HTMLでもおなじみだと思いますが、img要素なら「src」、「alt」、「width」、「height」等が該当します。
SONYのカーナビ用のポイントファイルデータの仕様は分かりませんが、<multimedia format="nav-u:sound" href="file://./nav_u_common/2" repeat="1">は、multimedia要素で「format」、「href」、「repeat」が属性と考えられますので、コメントしたようにaddAttribute()で一つずつ属性を追加します。
コメント(2件)
上記を次のように修正するといいのではないですか?
$multimedia->addAttribute('format', 'nav-u:sound');
$multimedia->addAttribute('href', 'file://./nav_u_common/2');
$multimedia->addAttribute('repeat', '1');
追伸。ホイントを差し上げたく思いますので、回答に上記の答えをご記入いただけますか?
また、rouge_2008からご回答を得ましたので質問は終了させていただきます。