XML 文件 root.iter() 和 for 循环输出不同的结果



我很困惑为什么以下两个命令输出不同的结果:

root = gml.getroot()  # define an element tree
for child in root:
    print(child.tag, child.attrib)

这将在我的.xml文件中输出以下内容:

{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
...

但是,这个带有root.iter()的代码将输出不同的结果......

for elem in root.iter():
    print(elem.tag, elem.attrib)

返回

{http://www.opengis.net/citygml/2.0}CityModel {'{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'http://www.opengis.net/citygml/cityobjectgroup/2.0 http://schemas.opengis.net/citygml/cityobjectgroup/2.0/cityObjectGroup.xsd http://www.opengis.net/citygml/building/2.0 http://schemas.opengis.net/citygml/building/2.0/building.xsd http://www.opengis.net/citygml/tunnel/2.0 http://schemas.opengis.net/citygml/tunnel/2.0/tunnel.xsd http://www.opengis.net/citygml/waterbody/2.0 http://schemas.opengis.net/citygml/waterbody/2.0/waterBody.xsd http://www.opengis.net/citygml/appearance/2.0 http://schemas.opengis.net/citygml/appearance/2.0/appearance.xsd http://www.opengis.net/citygml/cityfurniture/2.0 http://schemas.opengis.net/citygml/cityfurniture/2.0/cityFurniture.xsd http://www.opengis.net/citygml/generics/2.0 http://schemas.opengis.net/citygml/generics/2.0/generics.xsd http://www.opengis.net/citygml/bridge/2.0 http://schemas.opengis.net/citygml/bridge/2.0/bridge.xsd http://www.opengis.net/citygml/vegetation/2.0 http://schemas.opengis.net/citygml/vegetation/2.0/vegetation.xsd http://www.opengis.net/citygml/transportation/2.0 http://schemas.opengis.net/citygml/transportation/2.0/transportation.xsd http://www.opengis.net/citygml/relief/2.0 http://schemas.opengis.net/citygml/relief/2.0/relief.xsd http://www.opengis.net/citygml/landuse/2.0 http://schemas.opengis.net/citygml/landuse/2.0/landUse.xsd'}
{http://www.opengis.net/citygml/2.0}cityObjectMember {}
{http://www.opengis.net/citygml/building/2.0}Building {'{http://www.opengis.net/gml}id': 'UUID_82d7797e-7082-4d1c-a2e1-95f566b8f692'}
{http://www.opengis.net/gml}boundedBy {}
{http://www.opengis.net/gml}Envelope {'srsName': 'urn:ogc:def:crs:EPSG::4326', 'srsDimension': '3'}
{http://www.opengis.net/gml}lowerCorner {}
{http://www.opengis.net/gml}upperCorner {}
{http://www.opengis.net/citygml/2.0}creationDate {}
{http://www.opengis.net/citygml/2.0}externalReference {}
{http://www.opengis.net/citygml/2.0}informationSystem {}
{http://www.opengis.net/citygml/2.0}externalObject {}
{http://www.opengis.net/citygml/2.0}name {}
{http://www.opengis.net/citygml/2.0}externalReference {}
{http://www.opengis.net/citygml/2.0}informationSystem {}
{http://www.opengis.net/citygml/2.0}externalObject {}
{http://www.opengis.net/citygml/2.0}name {}
{http://www.opengis.net/citygml/generics/2.0}intAttribute {'name': 'Region'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/generics/2.0}intAttribute {'name': 'QualitaetStatus'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/generics/2.0}stringAttribute {'name': 'Herkunft'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/generics/2.0}intAttribute {'name': 'GebaeudeStatus'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/generics/2.0}dateAttribute {'name': 'FileCreationDate'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/building/2.0}class {}
{http://www.opengis.net/citygml/building/2.0}consistsOfBuildingPart {}
{http://www.opengis.net/citygml/building/2.0}BuildingPart {'{http://www.opengis.net/gml}id': 'UUID_9c7467b2-96ab-4844-af76-95eeddc6c8d7'}
{http://www.opengis.net/citygml/2.0}creationDate {}
{http://www.opengis.net/citygml/generics/2.0}intAttribute {'name': 'Geomtype'}
{http://www.opengis.net/citygml/generics/2.0}value {}
{http://www.opengis.net/citygml/building/2.0}boundedBy {}
{http://www.opengis.net/citygml/building/2.0}WallSurface {'{http://www.opengis.net/gml}id': 'UUID_9f8df737-fe84-451c-aad9-803faaea66d2'}
...

似乎root.iter()会循环树中的每个子级和子级,而for循环只会遍历直接子级?

我也在寻找这两个功能的解释时遇到麻烦。我该如何参考他们的不同行为?

谢谢!

很抱歉发送垃圾邮件,我刚刚找到了有关Element.iter()的文档,确实"迭代器按文档(深度优先(顺序迭代此元素及其下的所有元素。猜猜我的困惑主要是由相似的 API 名称引起的。

以下是有关for循环用法的文档,第 19.7.1.2 节。

最新更新