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

时间:2019-03-26 21:52:10

标签: xml

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

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循环只会遍历树中的直属子级?

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

谢谢!

1 个答案:

答案 0 :(得分:0)

抱歉,我只是在Element.iter()上发现了the documentation,并且确实是“迭代器以文档(深度优先)的顺序遍历此元素及其下的所有元素。”主要是由相似的API名称引起的。

Herefor循环用法的文档,第19.7.1.2节。