Parse XML in Python

For personal work.

You can get the response in XML with the Ministry of Internal Affairs and Communications API. Here is a summary of how to parse this XML. I plan to use it for work when the beginning of the week is free. The sample code of the Ministry of Internal Affairs and Communications API describes how to get social and demographic statistics, and when you hit it, the following XML will be returned.

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<GET_STATS_DATA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.e-stat.go.jp/rest/2.0/schema/GetStatsData.xsd">
<RESULT>
<STATUS>0</STATUS>
<ERROR_MSG>It ended normally.</ERROR_MSG>
<DATE>2016-09-04T21:28:09.920+09:00</DATE>
</RESULT>
<PARAMETER>
<LANG>J</LANG>
<STATS_DATA_ID>C0020050213000</STATS_DATA_ID>
<NARROWING_COND>
<CODE_CAT01_SELECT>#A03503</CODE_CAT01_SELECT>
</NARROWING_COND>
<DATA_FORMAT>X</DATA_FORMAT>
Omitted below

It's like getting this using the urllib module and accessing the element using the xml module. If it is an old API, JSON output may not be available.

Import from API

This is written in various places, and if you look at the official document, you can find it in various places. The endpoint URL of the Ministry of Internal Affairs and Communications API is used, but please log in to obtain the application ID.

import urllib

url = 'http://api.e-stat.go.jp/rest/2.0/app/XXXXXXXXXXXX'
req = urllib.request.Request(url)

with urllib.request.urlopen(req) as response:
    XmlData = response.read()

This will store the XML text data in XmlData.

XML parsing

Import the XML module and start parsing. This is also detailed in the official documentation.

import xml.etree.ElementTree as ET

root = ET.fromstring(XmlData)

only this.

Take a look at the contents of the element

#Top level tags and contents
print(root.tag,root.attrib)

#Child hierarchy tags and contents
for child in root:
    print(child.tag, child.attrib)

If you want to include grandchild elements, isn't it better to create a list or dictionary and write it?

Access to elements

It's the same as accessing the list.

print(root[0][1].text)

I think I can do what I want to do for the time being.

Recommended Posts

Parse XML in Python
Parse User Agent in Python
Read Namespace-specified XML in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
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
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble 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
Get Precipitation Probability from XML in Python
Manipulate namespaced XML in Python (Element Tree)
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python