質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

934閲覧

kmlファイルを解析し<name></name>で囲まれている範囲の文字列を抽出したい

tom0125

総合スコア10

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/06/30 02:50

編集2019/06/30 06:45

前提・実現したいこと

kmlファイルを解析し<name></name>で囲まれている範囲の文字列を抽出したい

該当のソースコード

<kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>登録店舗</name> <description><![CDATA[地図の説明<br>]]></description> <Style id="icon-1541-FF5252-labelson"> <IconStyle> <color>ff5252ff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1541-FFEA00-labelson"> <IconStyle> <color>ff00eaff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1541-FFEA00-labelson-nodesc"> <IconStyle> <color>ff00eaff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> <BalloonStyle> <text><![CDATA[<h3>$[name]</h3>]]></text> </BalloonStyle> </Style> <Style id="icon-1639-0F9D58-labelson"> <IconStyle> <color>ff589d0f</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1639-0F9D58-labelson-nodesc"> <IconStyle> <color>ff589d0f</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> <BalloonStyle> <text><![CDATA[<h3>$[name]</h3>]]></text> </BalloonStyle> </Style> <Style id="icon-1644-0288D1-labelson"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1644-FF5252-labelson"> <IconStyle> <color>ff5252ff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1700-F57C00-labelson"> <IconStyle> <color>ff007cf5</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1739-0288D1-labelson"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> </Style> <Style id="icon-1739-0288D1-labelson-nodesc"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> </IconStyle> <BalloonStyle> <text><![CDATA[<h3>$[name]</h3>]]></text> </BalloonStyle> </Style> <Style id="icon-1899-0288D1-normal"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> </IconStyle> <LabelStyle> <scale>0</scale> </LabelStyle> </Style> <Style id="icon-1899-0288D1-highlight"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> </IconStyle> <LabelStyle> <scale>1</scale> </LabelStyle> </Style> <StyleMap id="icon-1899-0288D1"> <Pair> <key>normal</key> <styleUrl>#icon-1899-0288D1-normal</styleUrl> </Pair> <Pair> <key>highlight</key> <styleUrl>#icon-1899-0288D1-highlight</styleUrl> </Pair> </StyleMap> <Style id="icon-1899-0288D1-labelson-nodesc"> <IconStyle> <color>ffd18802</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> </IconStyle> <BalloonStyle> <text><![CDATA[<h3>$[name]</h3>]]></text> </BalloonStyle> </Style> <Style id="icon-1899-FF5252-labelson"> <IconStyle> <color>ff5252ff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> </IconStyle> </Style> <Style id="icon-1899-FF5252-labelson-nodesc"> <IconStyle> <color>ff5252ff</color> <scale>1</scale> <Icon> <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> </Icon> <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> </IconStyle> <BalloonStyle> <text><![CDATA[<h3>$[name]</h3>]]></text> </BalloonStyle> </Style> <Folder> <name>UberEats店舗位置.xlsx</name> <Placemark> <name>1101sai cafe</name> <address>福岡県福岡市中央区大手門3-1-13</address> <description>福岡県福岡市中央区大手門3-1-13</description> <styleUrl>#icon-1739-0288D1-labelson</styleUrl> </Placemark> <Placemark> <name>ABBEY</name> <address>福岡県福岡市中央区薬院2-4-5 103 大稲マンション1-B</address> <description>福岡県福岡市中央区薬院2-4-5 103 大稲マンション1-B</description> <styleUrl>#icon-1739-0288D1-labelson</styleUrl> </Placemark> <Placemark> <name>B.B.M バーガービッグマウンテン</name> <address>福岡県福岡市中央区薬院1-16-5</address> <description>福岡県福岡市中央区薬院1-16-5</description> <styleUrl>#icon-1739-0288D1-labelson</styleUrl> </Placemark>

試したこと

初歩的な質問になりますが、nokogiriでhtml,xml形式以外のファイルを解析することは可能なのでしょうか。
kmlファイル解析、x path指定方法等、ご教授頂ければ幸いです。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asm

2019/06/30 03:06

提示された断片はxmlとして不完全のようですが これで全部なのでしょうか? また、Nokogiriで読み込もうとした際のコードも提示していただけますか?
guest

回答1

0

ベストアンサー

<kml xmlns="http://www.opengis.net/kml/2.2">

によって、これ以下の<name>タグがxmlns="http://www.opengis.net/kml/2.2"下の`name`タグになっています。

ruby

1kml = Nokogiri::XML(File.open("KML_Samples.kml")).remove_namespaces! 2pp kml.xpath("//name").map(&:text)

remove_namespaces!については公式ドキュメントを参照してください

投稿2019/06/30 06:18

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問