Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

get the namespaces from xml with python ElementTree

I use python 2.7 with the lib ElementTree.

I can't use lxml lib.

I need to get the namespaces in a string namespace_string. In order to fill my namespace dictionary.

my 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>

code:

import clr
import sys

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append("C:Program Files (x86)IronPython 2.7Lib")
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'])])

The code for the dictionary is from this answer:https://stackoverflow.com/a/37409050/7317684

I tryed namespace_string=root.tag but this get me only ="{http://www.adv-online.de/namespaces/adv/gid/6.0}AX_Bestandsdatenauszug"

Found only lxml solutions:(

Any help would be great!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code for creating a dictionary with all the declared namespaces can be made quite simple. This is all that is needed:

import xml.etree.ElementTree as ET

my_namespaces = dict([node for _, node in ET.iterparse('file.xml',
                                                        events=['start-ns'])])

You don't need to use StringIO or open(). Just provide the XML filename as an argument to iterparse().

Each item provided by iterparse() is an (event, (prefix, namespace-uri)) tuple. The start-ns event is not described in the Python 2.7 documentation of iterparse (but it is mentioned in the corresponding Python 3 documentation).


Note: the code above works in CPython and Jython, but not in IronPython. See https://github.com/IronLanguages/main/issues/968.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...