pythonでxmlファイルを読み込む方法の解析


pythonについてxmlの文章を読むことが多いですが、大体の文章は一つのxmlファイルを貼ってから処理ファイルのコードを貼ります。このように初心者の学習には不利です。この文章はpythonを使ってxmlファイルを読み取る方法をもっと分かりやすく教えてほしいです。
何がxmlですか?
xmlはタグ言語を拡張します。データをマーキングしたり、データの種類を定義したりして、ユーザーが自分のタグ言語を定義できるソース言語です。
abc.xml

<?xml version="1.0" encoding="utf-8"?>
<catalog>
  <maxid>4</maxid>
  <login username="pytest" passwd='123456'>
    <caption>Python</caption>
    <item id="4">
      <caption>  </caption>
    </item>
  </login>
  <item id="2">
    <caption>Zope</caption>
  </item>
</catalog>
Okは、構造上、私たちがよく使うHTMLハイパーテキストマーク言語に似ています。しかし、彼らが設計された目的は違っています。ハイパーテキストマーク言語はデータを表示するように設計されています。その焦点はデータの外観です。データの内容に焦点を当ててデータを伝送し記憶するように設計されています。
では、次のような特徴があります。
まず、ラベルペアがあります。
タグには属性があります。
ラベルペアはデータを埋め込むことができます。
ラベルはサブタグを埋め込むことができます。



ラベルの属性を取得
では、このタイプのファイルをpythonで読み込む方法を紹介します。

#coding=utf-8
import xml.dom.minidom

#  xml  
dom = xml.dom.minidom.parse('abc.xml')

#        
root = dom.documentElement
print root.nodeName
print root.nodeValue
print root.nodeType
print root.ELEMENT_NODE
mxl.dom.minidomモジュールはxmlファイルを処理するために使われますので、先に導入します。
xml.dom.minidom.parse()は、xmlファイルを開いて、このファイルのオブジェクトdom変数を指定します。
documentElementは、domオブジェクトのドキュメント要素を取得し、そのオブジェクトをrootに与えます。
各結点にはそのnodeName、nodeValue、nodeType属性があります。
nodeNameは結点の名前です。
nodeValueは結点の値で、テキストの結点にのみ有効です。
nodeTypeは結点のタイプです。catalogはELINE_ですNOのタイプ
現在は以下の種類があります。
'ATTRIBUTE_NODE
'CDATA_SECTIONNODE
'COMMENT_NODE
'DOCUMENT_FRAGMENTNODE
'DOCUMENT_NODE
'DOCUMENT_TYPE_NODE
'ELIENT_NODE
'ENTITY_NODE
'ENTITY_REFERENCE_NODE
'NOTATION_NODE
'PROCESSING_INSTRUCTIONNODE
'TEXT_NODE
ノードType-有名定数
http://www.w3school.com.cn/xmldom/dom_nodetype.asp
サブタグを取得
今catalogのサブラベルを獲得します。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>  </caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>
要素の名前を知るサブ要素については、getElements ByTagName法を用いて取得することができる。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>  </caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>
同じラベルの名前のラベルはどうやって区別しますか?

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>  </caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>
<caption>と<item>ラベルは一つしかないですが、どうやって区別しますか?

#coding=utf-8
import xml.dom.minidom

#  xml  
dom = xml.dom.minidom.parse('abc.xml')

#        
root = dom.documentElement

bb = root.getElementsByTagName('caption')
b= bb[2]
print b.nodeName

bb = root.getElementsByTagName('item')
b= bb[1]
print b.nodeName
root.getElementsByTagName('caption')はタグがcaptionの一組のラベルで、b[0]は一組のタグの中の一番目を表します。b[2]は、このグループのラベルの3番目を表します。
ラベルの属性値を取得します。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>  </caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>
<ロゴ>と<アイテム>のタグには属性がありますが、どのように属性を取得しますか?

#coding=utf-8
import xml.dom.minidom

#  xml  
dom = xml.dom.minidom.parse('abc.xml')

#        
root = dom.documentElement

itemlist = root.getElementsByTagName('login')
item = itemlist[0]
un=item.getAttribute("username")
print un
pd=item.getAttribute("passwd")
print pd

ii = root.getElementsByTagName('item')
i1 = ii[0]
i=i1.getAttribute("id")
print i

i2 = ii[1]
i=i2.getAttribute("id")
print i
get Attribute方法は元素の属性に対応する値を得ることができます。
ラベルペア間のデータを取得します。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <maxid>4</maxid>
    <login username="pytest" passwd='123456'>
        <caption>Python</caption>
       <item id="4">
          <caption>  </caption>
      </item>
  </login>
  <item id="2">
      <caption>Zope</caption>
  </item>
</catalog>
<caption>ラベルペアの間にデータがありますが、これらのデータはどうやって得られますか?
ラベルペア間のデータを得るためには様々な方法があります。
方法1

#coding=utf-8
import xml.dom.minidom

#  xml  
dom = xml.dom.minidom.parse('abc.xml')

#        
root = dom.documentElement

cc=dom.getElementsByTagName('caption')
c1=cc[0]
print c1.firstChild.data

c2=cc[1]
print c2.firstChild.data

c3=cc[2]
print c3.firstChild.data
first_Child属性は、選択されたノードの第1のサブノードに戻り、dataはノード人データを取得することを示す。
方法2

#coding=utf-8
from xml.etree import ElementTree as ET
per=ET.parse('abc.xml')
p=per.findall('./login/item')

for oneper in p:
  for child in oneper.getchildren():
    print child.tag,':',child.text


p=per.findall('./item')

for oneper in p:
  for child in oneper.getchildren():
    print child.tag,':',child.text
方法2はやや複雑で、参照モジュールも前のものとは違って、どのレベルのラベルの下で始まったかを指定するためにfindllを使用します。
getchection drenメソッドは、ドキュメント順にすべてのサブラベルを返します。ラベル名(child.tag)とラベルのデータ(child.text)を出力します。
実際には、方法二の役割はここではなく、コア機能は、あるレベルのラベルの下にあるすべてのサブタグを遍歴することができます。
ここではpythonでxmlファイルの解析方法に関する記事を紹介します。pythonでxmlファイルの内容を読み取ります。以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。