# XML. パース

# Python

xml.etree.ElementTree を使う

import xml.etree.ElementTree as ET

tree = ET.parse(kml_file)
root = tree.getroot()

NS = 'http://www.opengis.net/kml/2.2'

for folder in root[0].findall('./ns:Folder', {'ns' : NS}):
  for placemark in folder[1:]:
    ..

みたいな感じ

# Ruby

rexml/document を使う

elem = REXML::Document.new(File.open(fname))

# 属性
elem.attributes.each {|k, v|
  puts "#{k} : #{v}"
}

# 値
text = elem.text.to_s
puts text

# 子要素
elem.elements.each {|e|
  ..
}

# xpath 1
val = doc.elements[xpath].attributes[key]
puts val

# xpath 2
REXML::XPath.match(doc, xpath).each {|e|
  val = e.attributes[key]
  puts val
}

[ 📩 ご意見 ]