本文整理匯總了Python中xml.etree.ElementTree._namespace_map方法的典型用法代碼示例。如果您正苦於以下問題:Python ElementTree._namespace_map方法的具體用法?Python ElementTree._namespace_map怎麽用?Python ElementTree._namespace_map使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree._namespace_map方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _ET_tostring
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def _ET_tostring(e):
"""Between Python v2 and v3, there are some differences in the ElementTree library's tostring() behavior. One, the method balks at the "unicode" encoding in v2. Two, in 2, the XML prototypes output with every invocation. This method serves as a wrapper to deal with those issues, plus another issue where ET.tostring prints repeated xmlns declarations (observed on reading and writing a DFXML file twice in the same process). The repeated prints appear to be from a lack of inspection of existing namespace declarations in the attributes dictionary."""
retval = None
if sys.version_info[0] < 3:
tmp = ET.tostring(e, encoding="UTF-8")
if tmp[0:2] == "<?":
#Trim away first line; it's an XML prototype. This only appears in Python 2's ElementTree output.
retval = tmp[ tmp.find("?>\n")+3 : ]
else:
retval = tmp
else:
retval = ET.tostring(e, encoding="unicode")
container_end = retval.index(">")
for (uri, prefix) in list(ET._namespace_map.items()):
if prefix == "":
xmlns_attr_name = "xmlns"
else:
xmlns_attr_name = "xmlns:" + prefix
xmlns_attr_string = '%s="%s"' % (xmlns_attr_name, uri)
xmlns_attr_tally = retval.count(xmlns_attr_string, 0, container_end)
if xmlns_attr_tally > 1:
_logger.warning("ET.tostring() printed a repeated xmlns declaration: %r. Trimming %d repetition(s)." % (xmlns_attr_string, xmlns_attr_tally-1))
container_string = retval[ : container_end+1 ]
retval = container_string.replace(xmlns_attr_string, "", xmlns_attr_tally-1) + retval[ container_end+1 : ]
return retval
示例2: GetFeaturesInBbox
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def GetFeaturesInBbox(bbox):
#featureType = "DigitalGlobe:CollectedContent"
featureType = "DigitalGlobe:FinishedFeature"
url = "%s&request=GetFeature&typeName=%s&srsName=EPSG:4326&Bbox=%s" % (wfs2_url, featureType, ",".join(str(x) for x in bbox))
print url
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
#ET._namespace_map["http://www.digitalglobe.com"] = 'DigitalGlobe'
#ET._namespace_map["http://www.opengis.net/gml"] = 'gml'
try:
response = urllib2.urlopen(request)
print "Info:", response.info()
result = response.read()
print result
except urllib2.HTTPError as e:
print '*** WFS Server error', e.code(), e.read()
示例3: __enter__
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
示例4: __exit__
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
示例5: register_namespace
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def register_namespace(prefix, uri):
from xml.etree import ElementTree
# cElementTree uses ElementTree's _namespace_map, so that's ok
ElementTree._namespace_map[uri] = prefix
示例6: add_namespace
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def add_namespace(self, prefix, url):
"""In case of conflicting namespace definitions, first definition wins."""
#_logger.debug("self._namespaces.keys() = %r." % self._namespaces.keys())
if prefix not in self._namespaces.keys():
#_logger.debug("Registering namespace: %r, %r." % (prefix, url))
self._namespaces[prefix] = url
ET.register_namespace(prefix, url)
#_logger.debug("ET namespaces after registration: %r." % ET._namespace_map)
示例7: FindCollectedFeatures
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def FindCollectedFeatures(bbox):
featureType = "CollectedContent"
#featureType = "DigitalGlobe:FinishedFeature"
url = "%s&request=GetFeature&typeName=%s&srsName=EPSG:4326&bbox=%s" % (wfs2_url, featureType, ",".join(str(x) for x in bbox))
print url
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
#ET._namespace_map["http://www.digitalglobe.com"] = 'DigitalGlobe'
#ET._namespace_map["http://www.opengis.net/gml"] = 'gml'
try:
response = urllib2.urlopen(request)
#print "Info:", response.info()
result = response.read()
print result
#root = ET.fromstring(result)
#for child in root:
# print child.tag, child.attrib
#namespaces = {'gml': 'http://www.opengis.net/gml', 'DigitalGlobe': "http://www.digitalglobe.com"} # add more as needed
#posList = root.find('gml:featureMembers/DigitalGlobe:FinishedFeature/DigitalGlobe:geometry/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList', namespaces).text
#print posList
#arr = posList.split(" ")
#bbox = [ float(arr[1]), float(arr[0]), float(arr[5]), float(arr[4])]
#print bbox
#return bbox
except urllib2.HTTPError as e:
print '*** WFS Server error', e.code(), e.read()
示例8: FindFeature
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def FindFeature(identifier):
featureType = "DigitalGlobe:FinishedFeature"
#url = "%s&request=GetFeature&typeName=%s&BBOX=-64.8,32.3,-64.7,32.4&srsName=EPSG:4326" % (wfs_url, featureType)
#url = "%s&request=GetFeature&typeName=%s&BBOX=30.250383448012,-97.7684708569195,30.3119534076606,-97.7092977813448&WIDTH=3000&HEIGHT=3000&srsName=EPSG:4326" % (wfs_url, featureType)
url = "%s&request=GetFeature&typeName=%s&srsName=EPSG:4326&CQL_Filter=featureId='%s'" % (wfs_url, featureType, identifier)
#print url
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
#ET._namespace_map["http://www.digitalglobe.com"] = 'DigitalGlobe'
#ET._namespace_map["http://www.opengis.net/gml"] = 'gml'
try:
response = urllib2.urlopen(request)
#print "Info:", response.info()
result = response.read()
root = ET.fromstring(result)
#for child in root:
# print child.tag, child.attrib
namespaces = {'gml': 'http://www.opengis.net/gml', 'DigitalGlobe': "http://www.digitalglobe.com"} # add more as needed
posList = root.find('gml:featureMembers/DigitalGlobe:FinishedFeature/DigitalGlobe:geometry/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList', namespaces).text
#print posList
arr = posList.split(" ")
bbox = [ float(arr[1]), float(arr[0]), float(arr[5]), float(arr[4])]
#print bbox
return bbox
except urllib2.HTTPError as e:
print '*** WFS Server error', e.code(), e.read()
示例9: test_main
# 需要導入模塊: from xml.etree import ElementTree [as 別名]
# 或者: from xml.etree.ElementTree import _namespace_map [as 別名]
def test_main(module=None):
# When invoked without a module, runs the Python ET tests by loading pyET.
# Otherwise, uses the given module as the ET.
if module is None:
module = pyET
global ET
ET = module
test_classes = [
ModuleTest,
ElementSlicingTest,
BasicElementTest,
BadElementTest,
BadElementPathTest,
ElementTreeTest,
IOTest,
ParseErrorTest,
XIncludeTest,
ElementTreeTypeTest,
ElementFindTest,
ElementIterTest,
TreeBuilderTest,
XMLParserTest,
BugsTest,
]
# These tests will only run for the pure-Python version that doesn't import
# _elementtree. We can't use skipUnless here, because pyET is filled in only
# after the module is loaded.
if pyET is not ET:
test_classes.extend([
NoAcceleratorTest,
])
# Provide default namespace mapping and path cache.
from xml.etree import ElementPath
nsmap = pyET._namespace_map
# Copy the default namespace mapping
nsmap_copy = nsmap.copy()
# Copy the path cache (should be empty)
path_cache = ElementPath._cache
ElementPath._cache = path_cache.copy()
try:
support.run_unittest(*test_classes)
finally:
from xml.etree import ElementPath
# Restore mapping and path cache
nsmap.clear()
nsmap.update(nsmap_copy)
ElementPath._cache = path_cache
# don't interfere with subsequent tests
ET = None