我不明白为什么以下两个命令输出不同的结果:
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循环只循环直接子循环?
我也很难找到这两个功能的解释。我怎样才能提及他们的不同行为?
谢谢!
发布于 2019-03-26 21:57:06
对于垃圾邮件,我很抱歉,我刚刚在Element.iter()上找到了Element.iter(),实际上,“迭代器按照文档(深度优先)的顺序迭代这个元素和它下面的所有元素”。我猜我的困惑主要是由类似的API名称造成的。
这里是关于for循环使用的文档,第19.7.1.2节。
https://stackoverflow.com/questions/55366725
复制相似问题