Manipulate namespaced XML in Python (Element Tree)

at first

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 data

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.

Program code

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 **.

Commentary

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.

Yurubo

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). .. ..

reference

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

Manipulate namespaced XML in Python (Element Tree)
Parse XML in Python
Read Namespace-specified XML in Python
Manipulate files and folders in Python
Compiler in Python: PL / 0 syntax tree
Manipulate OCTA udf filenames in Python
Algorithm (segment tree) in Python (practice)
Rectangle area element split in Python
Output tree structure of files in Python
Delayed segment tree in Python (debug request)
Get Precipitation Probability from XML in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Compiler in Python: PL / 0 Abstract Syntax Tree (AST)
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
2. Multivariate analysis spelled out in Python 7-3. Decision tree [regression tree]
[Python] Get element by specifying name attribute in BeautifulSoup
2. Multivariate analysis spelled out in Python 7-1. Decision tree (scikit-learn)
I want to manipulate strings in Kotlin like Python!
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python