我有这个xml:
有多个"成分"标签。我需要遍历每个级别:
import sys
from xml.etree import ElementTree as et
base="<ss><cod>cod1</cod><measure><m>1</m></measure><constituents><cod>const1</cod><measure><m>2</m></measure><constituents><cod>const1_1</cod><measure><m>3</m></measure><constituents><cod>const3</cod><measure><m>4</m></measure></constituents></constituents><constituents><cod>const1_2</cod><measure><m>3</m></measure><constituents><cod>const3</cod><measure><m>42</m></measure></constituents></constituents></constituents></ss>"
tsString = et.fromstring(base)
ss=tsString.getiterator('ss')
for r in ss:
measure = risp.findall('.//constituents') #(1) get const1, const1_1, const3, const1_2, const3_2, only needed is const1
for c in measure:
measure1 = c.findall('.//constituents') #(2) get const1_1, const3, const1_2, const3_2, only needed are const1_1, const1_2
....
但是findall
会返回每个成员的出现。
我需要(1)findall只返回&#34; const1&#34;测量,(2)仅返回&#34; const1_1&#34;和&#34; const1_2&#34; (3)&#34; const3&#34;,&#34; const3_2&#34;
如何修复2个findall?
答案 0 :(得分:2)
只需省略.//
部分即可在当前节点中执行非递归搜索:
for r in ss:
measure = r.findall('constituents')
for c in measure:
measure1 = c.findall('constituents')
答案 1 :(得分:0)
.//
获得所有孩子。只需获得下一步&#34;步骤&#34;。
import sys
from xml.etree import ElementTree as et
base="<ss><cod>cod1</cod><measure><m>1</m></measure><constituents><cod>const1</cod><measure><m>2</m></measure><constituents><cod>const1_1</cod><measure><m>3</m></measure><constituents><cod>const3</cod><measure><m>4</m></measure></constituents></constituents><constituents><cod>const1_2</cod><measure><m>3</m></measure><constituents><cod>const3</cod><measure><m>42</m></measure></constituents></constituents></constituents></ss>"
tsString = et.fromstring(base)
ss=tsString.getiterator('ss')
for r in ss:
measure = r.findall('./constituents') #(1) get const1, const1_1, const3, const1_2, const3_2, only needed is const1
for t in measure: #for test
print t[0].text # for test
for c in measure:
measure1 = c.findall('./constituents') #(2) get const1_1, const3, const1_2, const3_2, only needed are const1_1, const1_2
for t in measure1: # for test
print t[0].text # for test