Python 用Pykml解析XML

Python 用Pykml解析XML,python,xml,kml,pykml,Python,Xml,Kml,Pykml,我从QGIS获得了以下xml文件 <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2&q

我从QGIS获得了以下xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
    <name>stationpivot.kml</name>
    <StyleMap id="default0">
        <Pair>
            <key>normal</key>
            <styleUrl>#default</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#hl</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="hl">
        <IconStyle>
            <scale>0.7</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <scale>0.7</scale>
        </LabelStyle>
    </Style>
    <Style id="default">
        <IconStyle>
            <scale>0.7</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <scale>0.7</scale>
        </LabelStyle>
    </Style>
    <Folder>
        <name>stationXML</name>
        <open>1</open>
        <Placemark>
            <name>2</name>
            <Snippet maxLines="0"></Snippet>
            <description><![CDATA[<html><body><table border="1">
<tr><th>Field Name</th><th>Field Value</th></tr>
<tr><td>Latitude</td><td>26.719803</td></tr>
<tr><td>Longitude</td><td>40.861876</td></tr>
<tr><td>Name</td><td>REALNAME2</td></tr>
<tr><td>Vegetation</td><td>v_type2</td></tr>
<tr><td>Description</td><td>text text text text</td></tr>
<tr><td>Time Description</td><td>time time time </td></tr>
</table></body></html>]]></description>
            <styleUrl>#default0</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>40.861876,26.71980299999999,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>3</name>
            <Snippet maxLines="0"></Snippet>
            <description><![CDATA[<html><body><table border="1">
<tr><th>Field Name</th><th>Field Value</th></tr>
<tr><td>Latitude</td><td>46.745151</td></tr>
<tr><td>Longitude</td><td>10.788845</td></tr>
<tr><td>Name</td><td>REALNAME3</td></tr>
<tr><td>Vegetation</td><td>v_type3</td></tr>
<tr><td>Description</td><td>text text text text</td></tr>
<tr><td>Time Description</td><td>time time time</td></tr>
</table></body></html>]]></description>
            <styleUrl>#default0</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>40.788845,26.74515100000001,0</coordinates>
            </Point>
        </Placemark>
      </Folder>
    </Document>
    </kml>

stationpivot.kml
典型的
#违约
突出
#hl
0.7
http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png
0.7
0.7
http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png
0.7
stationXML
1.
2.
字段名字段值
纬度26.719803
纵向40.861876
名称RealName2
植被V_类型2
DescriptionContext文本
时间描述时间
]]>
#默认值0
1.
40.861876,26.71980299999999,0
3.
字段名字段值
纬度46.745151
纵向10.788845
名称RealName3
植被V_类型3
DescriptionContext文本
时间描述时间
]]>
#默认值0
1.
40.788845,26.74515100000001,0
我想递归地替换

2
3.
使用“描述”字段REALNAME2中包含的信息的字段 为了拥有

<name>REALNAME2</name>
<name>REALNAME3</name>
REALNAME2
实名3
分别作为我的kml中的最终输出


有什么建议吗?

我建议您与一起使用。它很容易使用,而且非常强大。它将使您能够做您想做的事情:

import xml.etree.ElementTree as ET

root = ET.fromstring(<your KML as string>)
name_list = root.findall(".//Placemark/name")
for name in name_list:
    name.text = "Some new text"
将xml.etree.ElementTree作为ET导入
root=ET.fromstring()
name\u list=root.findall(“.//Placemark/name”)
对于名称列表中的名称:
name.text=“一些新文本”

使用pykml模块更新地名并创建新KML文件的完整代码

from pykml import parser
import re
import lxml.etree as et

with open('test.kml', 'r') as f:
  doc = parser.parse(f)

for pm in doc.getroot().Document.Folder.Placemark:
  # look for realname in description in markup
  # <tr><td>Name</td><td>*REALNAME2*</td></tr>
  r = re.search(r'<tr><td>Name</td><td>([^<]+)', pm.description.text)
  if r:
    pm.name = r.group(1)

# output new KML file
with open('out.kml', 'wb') as output:
  output.write(et.tostring(doc, pretty_print=True))
来自pykml导入解析器的

进口稀土
将lxml.etree作为et导入
以open('test.kml','r')作为f:
doc=parser.parse(f)
对于doc.getroot().Document.Folder.Placemark中的pm:
#在标记的描述中查找realname
#名称*REALNAME2*

r=re.search(r'Name)([^我试过了,但总是得到错误的结果!如果您能尽快创建一个示例,我将不胜感激。@Antoine我试图按照您的建议进行操作,但在使用以下代码时得到了空字段:root\u string=ET.fromstring(file\u xml)Name\u list=root\u string.findall(“.//Placemark/Name”)打印名称\u列表[]打印根\u字符串
from pykml import parser
import re
import lxml.etree as et

with open('test.kml', 'r') as f:
  doc = parser.parse(f)

for pm in doc.getroot().Document.Folder.Placemark:
  # look for realname in description in markup
  # <tr><td>Name</td><td>*REALNAME2*</td></tr>
  r = re.search(r'<tr><td>Name</td><td>([^<]+)', pm.description.text)
  if r:
    pm.name = r.group(1)

# output new KML file
with open('out.kml', 'wb') as output:
  output.write(et.tostring(doc, pretty_print=True))