使用元素树解析 wsdl(从定义中检索名称空间)

发布于 2024-12-17 06:53:45 字数 996 浏览 2 评论 0原文

我正在尝试使用 ElementTree 解析 wsdl 文件,作为其中的一部分,我想从给定的 wsdl 定义元素中检索所有命名空间。

例如,在下面的代码片段中,我正在尝试检索定义标记中的所有命名空间

<?xml version="1.0"?>
<definitions name="DateService" targetNamespace="http://dev-b.handel-dev.local:8080/DateService.wsdl" xmlns:tns="http://dev-b.handel-dev.local:8080/DateService.wsdl"
  xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:myType="DateType_NS" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

我的代码如下所示

import xml.etree.ElementTree as ET

xml_file='<path_to_my_wsdl>'
tree = xml.parse(xml_file)
rootElement = tree.getroot()
print (rootElement.tag)       #{http://schemas.xmlsoap.org/wsdl/}definitions
print(rootElement.attrib)     #targetNamespace="http://dev-b..../DateService.wsdl"

据我了解,在 ElementTree 中,名称空间 URI 与元素的本地名称相结合。如何从定义元素中检索所有名称空间条目

PS

:我是新的 ? (非常!)到 python

I am trying to parse a wsdl file using ElementTree, As part of this I"d like to retrieve all the namespaces from a given wsdl definitions element.

For instance in the below snippet , I am trying to retrieve all the namespaces in the definitions tag

<?xml version="1.0"?>
<definitions name="DateService" targetNamespace="http://dev-b.handel-dev.local:8080/DateService.wsdl" xmlns:tns="http://dev-b.handel-dev.local:8080/DateService.wsdl"
  xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:myType="DateType_NS" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

My code looks like this

import xml.etree.ElementTree as ET

xml_file='<path_to_my_wsdl>'
tree = xml.parse(xml_file)
rootElement = tree.getroot()
print (rootElement.tag)       #{http://schemas.xmlsoap.org/wsdl/}definitions
print(rootElement.attrib)     #targetNamespace="http://dev-b..../DateService.wsdl"

As I understand, in ElementTree the namespace URI is combined with the local name of the element .How can I retrieve all the namespace entries from the definitions element?

Appreciate your help on this

P.S: I am new (very!) to python

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-12-24 06:53:45
>>> import xml.etree.ElementTree as etree
>>> from StringIO import StringIO
>>>
>>> s = """<?xml version="1.0"?>
... <definitions
...   name="DateService"
...   targetNamespace="http://dev-b.handel-dev.local:8080/DateService.wsdl"
...   xmlns:tns="http://dev-b.handel-dev.local:8080/DateService.wsdl"
...   xmlns="http://schemas.xmlsoap.org/wsdl/"
...   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
...   xmlns:myType="DateType_NS"
...   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
...   xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
... </definitions>"""
>>> file_ = StringIO(s)
>>> namespaces = []
>>> for event, elem in etree.iterparse(file_, events=('start-ns',)):
...     print elem
...
(u'tns', 'http://dev-b.handel-dev.local:8080/DateService.wsdl')
('', 'http://schemas.xmlsoap.org/wsdl/')
(u'soap', 'http://schemas.xmlsoap.org/wsdl/soap/')
(u'myType', 'DateType_NS')
(u'xsd', 'http://www.w3.org/2001/XMLSchema')
(u'wsdl', 'http://schemas.xmlsoap.org/wsdl/')

受到 ElementTree 文档 的启发

>>> import xml.etree.ElementTree as etree
>>> from StringIO import StringIO
>>>
>>> s = """<?xml version="1.0"?>
... <definitions
...   name="DateService"
...   targetNamespace="http://dev-b.handel-dev.local:8080/DateService.wsdl"
...   xmlns:tns="http://dev-b.handel-dev.local:8080/DateService.wsdl"
...   xmlns="http://schemas.xmlsoap.org/wsdl/"
...   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
...   xmlns:myType="DateType_NS"
...   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
...   xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
... </definitions>"""
>>> file_ = StringIO(s)
>>> namespaces = []
>>> for event, elem in etree.iterparse(file_, events=('start-ns',)):
...     print elem
...
(u'tns', 'http://dev-b.handel-dev.local:8080/DateService.wsdl')
('', 'http://schemas.xmlsoap.org/wsdl/')
(u'soap', 'http://schemas.xmlsoap.org/wsdl/soap/')
(u'myType', 'DateType_NS')
(u'xsd', 'http://www.w3.org/2001/XMLSchema')
(u'wsdl', 'http://schemas.xmlsoap.org/wsdl/')

Inspired by the ElementTree documentation

画中仙 2024-12-24 06:53:45

您可以使用lxml

from lxml import etree
tree = etree.parse(file)
root = tree.getroot()
namespaces = root.nsmap

请参阅https://stackoverflow.com/a/26807636/5375693

You can use lxml.

from lxml import etree
tree = etree.parse(file)
root = tree.getroot()
namespaces = root.nsmap

see https://stackoverflow.com/a/26807636/5375693

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文