xml.etree.ElementTree http://docs.python.jp/2/library/xml.etree.elementtree.html Use to parse the namespaced xml.
In the text, kml (format used for Google Earth etc.) http://ja.wikipedia.org/wiki/KML Is targeted, but since the actual situation is XML, it is treated in the same way.
Target KML
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<name>Hoge</name>
<Style id="Mapit">
<IconStyle>
<color>FFFFFFFF</color>
<scale>1.0</scale>
</IconStyle>
</Style>
<Style id="kml">
<LineStyle>
<color>FF0080FF</color>
<width>1</width>
</LineStyle>
<PolyStyle>
<color>AADDDDDD</color>
</PolyStyle>
</Style>
・ ・ ・
</Document>
</kml>
Although some parts are omitted, for the time being, KML that has this shape is targeted. PolyStyle->color The value of is ** AADDDDDDD **, but I want to change this.
XML rewrite program
# -*- coding: utf-8 -*-
from xml.etree import ElementTree
#Read KML file
tree = ElementTree.parse("in.kml")
#Add namespace
#If you do not do this, ns0 will be added to all tags when rewriting.:Become like PolyStyle
ElementTree.register_namespace('', 'http://earth.google.com/kml/2.0')
# //PolyStyle/Find color
for node in tree.findall(".//{http://earth.google.com/kml/2.0}PolyStyle/{http://earth.google.com/kml/2.0}color"):
#Content rewriting
node.text="AA001122"
#Export
#If the third argument is true<?xml ...?>Is added.
tree.write("out.kml","UTF-8",True)
PolyStyle->color The value of is exported as ** AA001122 **.
xml.etree.ElementTree.findall has a "." At the beginning. (It is called Future Warning)
It seems that it can handle a subset of XPath. http://docs.python.jp/3/library/xml.etree.elementtree.html (No article about Python2 found)
The problem here is the namespace.
Namespace
<kml xmlns="http://earth.google.com/kml/2.0">
Is in the root, so everything below it takes a namespace.
So when you specify the path, you have to add the namespace inside {} and specify the tag.
Moreover, since it is PolyStyle-> color, I have to write it twice.
xpath
//{http://earth.google.com/kml/2.0}PolyStyle/{http://earth.google.com/kml/2.0}color"
(Please let me know if there is any good way ...)
Next, the meaning of register_namespace is The XML written by write
register_Output without namespace
<ns0:PolyStyle>
<ns0:color>AADDDDDD</ns0:color>
</ns0:PolyStyle>
Avoid becoming like.
XPath specification is smarter because it has register_namespace I wonder if it can be described in (I do not have to write the namespace URI many times). .. ..
How to get the next element with the text specified in XPath http://qiita.com/yuki2006/items/1f96450fc744769872c5
[Python] Gonyogonyo the namespace of ElementTree http://d.hatena.ne.jp/nullpobug/20110420/1303293319
Recommended Posts