- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 05:14 PM
皆様
いつもお世話になっております。
以下の点について、1点質問がございます。
①RestMessageを使って図書館APIから返ってきたXML形式の応答文を、リモートテーブルに格納する方法をご教授いただきたいです。(スクリプトの書き方についても、ご指摘いただけると幸いです。)
現状として、RestMessageを使って図書館APIから返ってきたJSON形式の応答文を、格納することには成功いたしました。また、RestMessageのXML形式の応答文は作成を行い、テストして確認済みです。
ざっくりとした質問で申し訳ございません。
以上です。よろしくお願いします。
XML形式の応答文です
<?xml version='1.0' encoding='utf-8'?>
<Libraries>
<Library>
<libid>101707</libid>
<formal>盛岡市移動図書館車「こずかた号」</formal>
<short>こずかた号</short>
<systemid>Iwate_Morioka</systemid>
<systemname>岩手県盛岡市</systemname>
<libkey>市立BM</libkey>
<category>BM</category>
<post>020-0114</post>
<tel>019-661-4343</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市高松一丁目9-45</address>
<geocode>141.1417011,39.7208255</geocode>
<isil />
<faid />
<url_pc>https://www.city.morioka.iwate.jp/kosodate/tosho/index.html</url_pc>
<image />
</Library>
<Library>
<libid>101706</libid>
<formal>盛岡市立図書館</formal>
<short>盛岡市立図書館</short>
<systemid>Iwate_Morioka</systemid>
<systemname>岩手県盛岡市</systemname>
<libkey>市立図書館</libkey>
<category>MEDIUM</category>
<post>020-0114</post>
<tel>019-661-4343</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市高松一丁目9-45</address>
<geocode>141.1417011,39.7208255</geocode>
<isil>JP-1000212</isil>
<faid />
<url_pc>https://www.city.morioka.iwate.jp/kosodate/tosho/index.html</url_pc>
<image />
</Library>
<Library>
<libid>101708</libid>
<formal>盛岡市渋民図書館</formal>
<short>渋民図書館</short>
<systemid>Iwate_Morioka</systemid>
<systemname>岩手県盛岡市</systemname>
<libkey>渋民図書館</libkey>
<category>MEDIUM</category>
<post>028-4132</post>
<tel>019-683-2167</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市玉山区渋民字鶴塚55</address>
<geocode>141.169992,39.849384</geocode>
<isil>JP-1000214</isil>
<faid />
<url_pc>https://www.city.morioka.iwate.jp/kosodate/tosho/index.html</url_pc>
<image />
</Library>
<Library>
<libid>101710</libid>
<formal>盛岡市移動図書館車「わかば号」</formal>
<short>わかば号</short>
<systemid>Iwate_Morioka</systemid>
<systemname>岩手県盛岡市</systemname>
<libkey>都南BM</libkey>
<category>BM</category>
<post>020-0834</post>
<tel>019-637-3636</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市永井24-90-2</address>
<geocode>141.1547357,39.6533056</geocode>
<isil />
<faid />
<url_pc>https://www.city.morioka.iwate.jp/kosodate/tosho/index.html</url_pc>
<image />
</Library>
<Library>
<libid>101709</libid>
<formal>盛岡市都南図書館</formal>
<short>都南図書館</short>
<systemid>Iwate_Morioka</systemid>
<systemname>岩手県盛岡市</systemname>
<libkey>都南図書館</libkey>
<category>MEDIUM</category>
<post>020-0834</post>
<tel>019-637-3636</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市永井24-90-2</address>
<geocode>141.1547357,39.6533056</geocode>
<isil>JP-1000213</isil>
<faid />
<url_pc>https://www.city.morioka.iwate.jp/kosodate/tosho/index.html</url_pc>
<image />
</Library>
<Library>
<libid>101725</libid>
<formal>岩手県立図書館</formal>
<short>岩手県立図書館</short>
<systemid>Iwate_Pref</systemid>
<systemname>岩手県立図書館</systemname>
<libkey>岩手県立図書館</libkey>
<category>LARGE</category>
<post>020-0045</post>
<tel>019-606-1730</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市盛岡駅西通一丁目7番1号 いわて県民情報交流センター「アイーナ」内</address>
<geocode>141.1327385,39.7019718</geocode>
<isil>JP-1000211</isil>
<faid>FA012830</faid>
<url_pc>http://www.library.pref.iwate.jp/</url_pc>
<image />
</Library>
<Library>
<libid>109453</libid>
<formal>農研機構図書館・東北農業研究センター</formal>
<short>東北農業研究センター</short>
<systemid>Special_Affrc</systemid>
<systemname>農林水産関係試験研究機関総合目録</systemname>
<libkey>東北農研セ</libkey>
<category>SPECIAL</category>
<post>020-0198</post>
<tel>029-838-7379 [中央図書館]</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市下厨川字赤平4</address>
<geocode>141.134101,39.7468033</geocode>
<isil />
<faid>FA009916</faid>
<url_pc>https://www.naro.affrc.go.jp/introduction/library/index.html</url_pc>
<image />
</Library>
<Library>
<libid>122302</libid>
<formal>農研機構図書館・果樹茶業研究部門(リンゴ研究拠点)</formal>
<short>果樹茶盛岡</short>
<systemid>Special_Affrc</systemid>
<systemname>農林水産関係試験研究機関総合目録</systemname>
<libkey>果樹茶盛岡</libkey>
<category>SPECIAL</category>
<post>020-0123</post>
<tel>029-838-7379 [中央図書館]</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市下厨川字鍋屋敷92-24</address>
<geocode>141.1352449,39.7658863</geocode>
<isil />
<faid>FA009756</faid>
<url_pc>http://www.naro.affrc.go.jp/introduction/library/</url_pc>
<image />
</Library>
<Library>
<libid>109498</libid>
<formal>森林総合研究所東北支所図書室</formal>
<short>森林総合研究所(東北)</short>
<systemid>Special_Affrc</systemid>
<systemname>農林水産関係試験研究機関総合目録</systemname>
<libkey>森総研東北</libkey>
<category>SPECIAL</category>
<post>020-0123</post>
<tel>019-641-2150</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市下厨川字鍋屋敷92-25</address>
<geocode>141.1323644,39.7678315</geocode>
<isil>JP-1005381</isil>
<faid>FA010201</faid>
<url_pc>http://www.ffpri.affrc.go.jp/thk/info/</url_pc>
<image />
</Library>
<Library>
<libid>106373</libid>
<formal>岩手大学図書館</formal>
<short>情報メディアセンター図書館</short>
<systemid>Univ_Iwate</systemid>
<systemname>岩手大学</systemname>
<libkey>図書館</libkey>
<category>UNIV</category>
<post>020-8550</post>
<tel>019-621-6082</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市上田三丁目18-8</address>
<geocode>141.137211,39.715828</geocode>
<isil>JP-1003262</isil>
<faid>FA001368</faid>
<url_pc>http://www.lib.iwate-u.ac.jp/</url_pc>
<image />
</Library>
<Library>
<libid>105693</libid>
<formal>岩手医科大学附属図書館</formal>
<short>附属図書館</short>
<systemid>Univ_Iwate_Med</systemid>
<systemname>岩手医科大学</systemname>
<libkey>内丸</libkey>
<category>UNIV</category>
<post>020-8505</post>
<tel>019-651-5110</tel>
<pref>岩手県</pref>
<city>盛岡市</city>
<address>岩手県盛岡市内丸19番1号</address>
<geocode>141.151661,39.704791</geocode>
<isil>JP-1003683</isil>
<faid>FA004413</faid>
<url_pc>https://www.iwate-med.ac.jp/library/</url_pc>
<image />
</Library>
</Libraries>
XML形式のリモートテーブル定義です。
※appkeyは変えています。
(function executeQuery(v_table, v_query) {
fetchAllIncidents(v_table, v_query);
/**
* fetch all incidents records from the remote instance
*/
function fetchAllIncidents(v_table, v_query) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
// Create a RestMessage first which calls an external REST service
try {
var r = new sn_ws.RESTMessageV2('x_686937_test_ma_0.kariru2 XML', 'GET');
r.setStringParameterNoEscape('appkey', 'appkey');
r.setStringParameterNoEscape('pref', '%E5%B2%A9%E6%89%8B%E7%9C%8C');
r.setStringParameterNoEscape('city', '%E7%9B%9B%E5%B2%A1%E5%B8%82');
var response = r.execute();
var responseBody = response.getBody();
var jsonObject = gs.xmlToJSON(responseBody);
//v_table.addRow();
if (response.haveError()) {
v_query.setLastErrorMessage(response.getErrorMessage());
// can use gs.error() or gs.addErrorMessage() while debugging
// gs.debug() messages visible in session debugger
// gs.debug(response.getErrorMessage());
return;
}
} catch (ex) {
v_query.setLastErrorMessage(ex.message);
// gs.debug(ex.message);
return;
}
}
} )(v_table, v_query);
参考までにJSON形式でのリモートテーブル定義です。
※appkeyは変えております。
fetchAllIncidents(v_table, v_query);
/**
* fetch all incidents records from the remote instance
*/
function fetchAllIncidents(v_table, v_query) {
// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
// Create a RestMessage first which calls an external REST service
try {
var r = new sn_ws.RESTMessageV2('x_686937_test_ma_0.kariru josn', 'GET');
r.setStringParameterNoEscape('pref', '%E5%B2%A9%E6%89%8B%E7%9C%8C');
r.setStringParameterNoEscape('appkey', 'appkey');
r.setStringParameterNoEscape('city', '%E7%9B%9B%E5%B2%A1%E5%B8%82');
r.setStringParameterNoEscape('format', '');
var response = r.execute();
var responseBody = response.getBody();
// 文字の置き換え
responseBody = responseBody.replace('callback(','{"list":');
// 文字の置き換え
responseBody = responseBody.replace(');','}') ;
//json形式に戻す
var rr = JSON.parse(responseBody);
// if REST call ends up in an error, set the last error message which shows up
// at the bottom of the list view
if (response.haveError()) { // <- should be checking for error right after getting a response.
v_query.setLastErrorMessage(response.getErrorMessage());
// can use gs.error() or gs.addErrorMessage() while debugging
// gs.debug() messages visible in session debugger
// gs.debug(response.getErrorMessage());
return;
}
// process body when there is no error.
for (var i = 0; i < rr.list.length; i++) {
var rec = {};
rec.pref = rr.list[i].pref;
rec.formal = rr.list[i].formal;
rec.city =rr.list[i].city;
// you can extract any other parameter.
v_table.addRow(rec);
}
} catch (ex) {
v_query.setLastErrorMessage(ex.message);
// gs.debug(ex.message);
return;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 11:27 PM
JSONのプログラムと、XMLのデータを参考に、プログラムを考えてみました。データ構造や名前が少々違う以外は同じ処理です。
var jsonObject = gs.xmlToJSON(responseBody);
var datalist = jsonObject.Libraries.Library;
for (var i = 0; i < datalist.length; i++) {
var rec = {};
rec.pref = datalist[i].pref;
rec.formal = datalist[i].formal;
rec.city = datalist[i].city;
// you can extract any other parameter.
v_table.addRow(rec);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 11:27 PM
JSONのプログラムと、XMLのデータを参考に、プログラムを考えてみました。データ構造や名前が少々違う以外は同じ処理です。
var jsonObject = gs.xmlToJSON(responseBody);
var datalist = jsonObject.Libraries.Library;
for (var i = 0; i < datalist.length; i++) {
var rec = {};
rec.pref = datalist[i].pref;
rec.formal = datalist[i].formal;
rec.city = datalist[i].city;
// you can extract any other parameter.
v_table.addRow(rec);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 05:01 PM
iwai様
回答ありがとうございます。
提示してくださったコードで、リモートテーブルにXML形式を格納することができました!
本当にありがとうございます。
質問なのですが、こちらの
var jsonObject = gs.xmlToJSON(responseBody);
はXML形式からJSON形式に変換している認識であっていますか?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 08:36 PM
- 質問『XML形式からJSON形式に変換している認識であっていますか?』
- 回答、XML形式で書かれた文字列をJSON形式の文字列に変換しているわけではなく、簡単に言うと、XMLの値を変数に格納しているだけと考えて良いです。
公式の説明では戻り値は『XML 文字列を表す JSON オブジェクト』と書かれているのですが、実際には、XMLの構造をObjectに変換した 普通の JavaScript の Object です。
GlideSystem xmlToJSON(String xmlString) | ServiceNow Developers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 10:51 PM