我用BatchGeo来创建电子表格中的地图,然后下载KML数据,这是取代它:使用正则表达式来找到一个字符串,然后使用正则表达式来寻找新的字符串
<Placemark>
<name>?</name>
<Snippet></Snippet>
<description><![CDATA[]]></description>
<styleUrl>#style75</styleUrl>
<address>1234 Example St Denver, CO 80221</address>
<Point>
<coordinates>-121.879364,37.815151,0.000000</coordinates>
</Point>
</Placemark>
当导入到Google地图中后,这些点将放置在正确的地址/坐标处,但左侧边栏上每个引脚旁边的名称/描述符只会显示“?”而不是显示地址。
我想用一个正则表达式找到每个"<name>?</name>"
,然后使用正则表达式查找的文件中<address>.*</address>
下一个实例,然后回去与*
那是<address>
标签之间更换<name>
标签之间的?
。
每个点的<Placemark>
标记之间有一段代码,总共有数百个点。
这里是星星点点我到目前为止有:
newkml = File.open('Newkml.txt', 'w')
def process_line(x)
unless x == "<name>?</name>"
# just return the original line
else
# Find the next instance of /<address>(.*)<\/address>/
# Go to the original line
# Replace it with "<name>#{$1}</name>"
end
end
File.foreach('Whatever.kml'){|line|} do line.process_line
# Make a new file, copy over all of the lines that aren't <name>?</name>,
# and fix the name lines using the method above
UPDATE:在原来的服务(BatchGeo)有设置里面有什么KML(XML)标签中有哪些信息的选项,所以我创建了一张新地图,并首先防止了该问题的发生。感谢那些向我推荐我可以在将来使用这种操作的工具。
更新2:尝试Mark Thomas的解决方案。这是我跑的代码:
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML("whatever.xml")
edits = 0
doc.xpath("//name").each do |name|
if name.content == "?"
name.content = name.xpath("following-sibling::address").text
edits +=1
end
end
puts(doc.inspect)
puts("edits: #{edits}")
puts doc
这给了我下面的输出:
#<Nokogiri::XML::Document:0xfe0064 name="document>
edits: 0
<?xml version="1.0"?>
如果我添加的作品,因为我认为它应该在edits
测试代码,这似乎表明if name.content == "?"
块执行0次(比我预期的少了130次)。
[Nokogiri](http://nokogiri.org/)。 – harbichidian
使用XML解析器来处理XML,就像echoback所建议的一样。 – nhahtdh
谢谢,我不知道这样的事情存在。现在阅读文档 - 看起来Nokogiri会有解决方案。我会在这里更新,如果我找到一个。 –