【发布时间】:2017-07-08 07:53:01
【问题描述】:
我使用带有 lib ElementTree 的 python 2.7。
我不能使用 lxml 库。
我需要在字符串namespace_string 中获取命名空间。为了填充我的命名空间字典。
我的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<AX_Bestandsdatenauszug
xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0"
xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gmd="http://www.isotc211.org/2005/gmd"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:ows="http://www.opengis.net/ows"
xmlns:wfs="http://www.adv-online.de/namespaces/adv/gid/wfs"
xmlns:wfsext="http://www.adv-online.de/namespaces/adv/gid/wfsext"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ogc="http://www.adv-online.de/namespaces/adv/gid/ogc"
xsi:schemaLocation="http://www.adv-online.de/namespaces/adv/gid/6.0 NAS-Operationen.xsd">
<enthaelt>
<gml:featureMember>
<xmlstuff>....a lot of xml stuff....</xmlstuff>
</gml:featureMember>
</enthaelt>
</AX_Bestandsdatenauszug>
代码:
import clr
import sys
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
import xml.etree.ElementTree as ET
from io import StringIO
xml="file.xml"
tree = ET.parse(xml)
root = tree.getroot()
my_schema = "namespace_string"
my_namespaces = dict([node for _, node in ET.iterparse(StringIO(my_schema), events=['start-ns'])])
字典的代码来自这个答案:https://stackoverflow.com/a/37409050/7317684
我尝试了namespace_string=root.tag,但这只让我 ="{http://www.adv-online.de/namespaces/adv/gid/6.0}AX_Bestandsdatenauszug"
只找到lxml解决方案:(
任何帮助都会很棒!
【问题讨论】:
标签: xml python-2.7 dictionary elementtree