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

问题描述 投票:0回答:1

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

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循环只会绕过直接的孩子?

我也很难找到这两个功能的解释。我怎样才能提到他们的不同行为?

谢谢!

xml
1个回答
0
投票

对于发送垃圾邮件感到抱歉,我刚刚在the documentation上发现了Element.iter(),而且“迭代器迭代了这个元素及其下面的所有元素,文档(深度优先)顺序。”猜猜我的混淆主要是由类似的API名称引起的。

Here是关于for循环使用的文档,第19.7.1.2节。

© www.soinside.com 2019 - 2024. All rights reserved.