LoginSignup
0
1

More than 1 year has passed since last update.

lxml.etree で XML の基本的なことを少し触ってみる

Last updated at Posted at 2022-06-06

目的

Windows 10 + Excel2016_x86 VBA で XML の基本的なことを少し触ってみる
で使用したXMLファイルをPythonで基本的な操作を実行してみる。

対象となるXMLデータ(addr2.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<results>
    <query>池尻4-35-25 </query>
    <geodetic>wgs1984</geodetic>
    <iConf>4</iConf>
    <converted>池尻4-35-</converted>
    <candidate>
        <address>東京都/世田谷区/池尻/四丁目/35番</address>
        <longitude>139.673965</longitude>
        <latitude>35.654259</latitude>
        <iLvl>7</iLvl>
    </candidate>
    <candidate>
        <address>兵庫県/伊丹市/池尻/四丁目/35番地</address>
        <longitude>135.381805</longitude>
        <latitude>34.784988</latitude>
        <iLvl>7</iLvl>
    </candidate>
</results>

Pythonのサンプルコード

基本的な機能を確認してみる

# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8
# https://lxml.de/tutorial.html

from lxml import etree

tree = etree.parse('./addr2.xml')
root = tree.getroot()

# 以下の2行は同一内容を返す
etree.dump(root)
print(etree.tostring(root, pretty_print=True, encoding='unicode'))

# 以下のコードで表示される内容
# query 池尻4-35-25 
# geodetic wgs1984
# iConf 4
# converted 池尻4-35-
# candidate
# address 東京都/世田谷区/池尻/四丁目/35番
# longitude 139.673965
# latitude 35.654259
# iLvl 7
# candidate
# address 兵庫県/伊丹市/池尻/四丁目/35番地
# longitude 135.381805
# latitude 34.784988
# iLvl 7

print(root[0].tag, root[0].text)
print(root[1].tag, root[1].text)
print(root[2].tag, root[2].text)
print(root[3].tag, root[3].text)
print("")
print(root[4].tag)
print(root[4][0].tag, root[4][0].text)
print(root[4][1].tag, root[4][1].text)
print(root[4][2].tag, root[4][2].text)
print(root[4][3].tag, root[4][3].text)
print("")
print(root[5].tag)
print(root[5][0].tag, root[5][0].text)
print(root[5][1].tag, root[5][1].text)
print(root[5][2].tag, root[5][2].text)
print(root[5][3].tag, root[5][3].text)

# 上記とほぼ同じ内容
children = list(root)
for elem in children:
    print(elem.tag, elem.text)
    for items in elem:
        print(items.tag, items.text)

# 以下のコードで表示される内容
# address 東京都/世田谷区/池尻/四丁目/35番
# longitude 139.673965
# latitude 35.654259
# iLvl 7
# address 兵庫県/伊丹市/池尻/四丁目/35番地
# longitude 135.381805
# latitude 34.784988
# iLvl 7

tree = root.findall('candidate')
for node in tree:
    print("")
    print(node[0].tag, node[0].text)
    print(node[1].tag, node[1].text)
    print(node[2].tag, node[2].text)
    print(node[3].tag, node[3].text)

tree = root.iter('candidate')
for node in tree:
    print("")
    for items in node:
        print(items.tag, items.text)

tree = root.xpath('candidate')
# tree = root.xpath('./candidate')
# tree = root.xpath('///candidate')
for node in tree:
    print("")
    print(node[0].tag, node[0].text)
    print(node[1].tag, node[1].text)
    print(node[2].tag, node[2].text)
    print(node[3].tag, node[3].text)

参考にしたのは以下のサイト

Excel の WEBSERVICE 関数でジオコーディング
xml.etree.ElementTree --- ElementTree XML API
Windows 10 + Excel2016_x86 VBA で XML の基本的なことを少し触ってみる
xml.etree.ElementTree で XML の基本的なことを少し触ってみる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1