Author: jprantan
Date: Mon Nov 24 01:44:29 2008
New Revision: 1072
Added:
trunk/src/robot/utils/etreedomwrapper.py
Log:
Added initial version of elementTree DomWrapper which should have huge
performance improvement compared to minidom implementation.
Added: trunk/src/robot/utils/etreedomwrapper.py
==============================================================================
--- (empty file)
+++ trunk/src/robot/utils/etreedomwrapper.py Mon Nov 24 01:44:29 2008
@@ -0,0 +1,55 @@
+# Copyright 2008 Nokia Siemens Networks Oyj
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+from StringIO import StringIO
+try:
+ import xml.etree.cElementTree as ET
+except ImportError:
+ try:
+ import xml.etree.ElementTree as ET
+ except ImportError:
+ try:
+ import cElementTree as ET
+ except ImportError:
+ import elementtree.ElementTree as ET
+
+from abstractdomwrapper import AbstractDomWrapper
+
+
+
+class DomWrapper(AbstractDomWrapper):
+
+ """A wrapper for Python's XML DOM for simplifying reading data from it.
+
+ See documentation of AbstractDomWrapper for further usage information.
+ """
+
+ def __init__(self, path=None, string=None, node=None):
+ """Initialize by giving 'path' to an xml file or xml as a 'string'.
+
+ Alternative initialization by giving dom 'node' ment to be used
only
+ internally. 'path' may actually also be an already opened file
object
+ (or anything accepted by ElementTree.parse).
+ """
+ # This should not be changed to if node:. See chapter Truth Testing
+ # from http://effbot.org/zone/element.htm#the-element-type
+ if node is None:
+ node = ET.parse(path or StringIO(string)).getroot()
+ AbstractDomWrapper.__init__(self, path)
+ self.name = node.tag
+ self.attrs = dict(node.items())
+ self.text = node.text or self.text
+ for child in list(node):
+ self.children.append(DomWrapper(node=child))