3 # $Id: ElementTree.py 2326 2005-03-17 07:45:21Z fredrik $
5 # light-weight XML support for Python 1.5.2 and later.
8 # 2001-10-20 fl created (from various sources)
9 # 2001-11-01 fl return root from parse method
10 # 2002-02-16 fl sort attributes in lexical order
11 # 2002-04-06 fl TreeBuilder refactoring, added PythonDoc markup
12 # 2002-05-01 fl finished TreeBuilder refactoring
13 # 2002-07-14 fl added basic namespace support to ElementTree.write
14 # 2002-07-25 fl added QName attribute support
15 # 2002-10-20 fl fixed encoding in write
16 # 2002-11-24 fl changed default encoding to ascii; fixed attribute encoding
17 # 2002-11-27 fl accept file objects or file names for parse/write
18 # 2002-12-04 fl moved XMLTreeBuilder back to this module
19 # 2003-01-11 fl fixed entity encoding glitch for us-ascii
20 # 2003-02-13 fl added XML literal factory
21 # 2003-02-21 fl added ProcessingInstruction/PI factory
22 # 2003-05-11 fl added tostring/fromstring helpers
23 # 2003-05-26 fl added ElementPath support
24 # 2003-07-05 fl added makeelement factory method
25 # 2003-07-28 fl added more well-known namespace prefixes
26 # 2003-08-15 fl fixed typo in ElementTree.findtext (Thomas Dartsch)
27 # 2003-09-04 fl fall back on emulator if ElementPath is not installed
28 # 2003-10-31 fl markup updates
29 # 2003-11-15 fl fixed nested namespace bug
30 # 2004-03-28 fl added XMLID helper
31 # 2004-06-02 fl added default support to findtext
32 # 2004-06-08 fl fixed encoding of non-ascii element/attribute names
33 # 2004-08-23 fl take advantage of post-2.1 expat features
34 # 2005-02-01 fl added iterparse implementation
35 # 2005-03-02 fl fixed iterparse support for pre-2.2 versions
37 # Copyright (c) 1999-2005 by Fredrik Lundh. All rights reserved.
39 # fredrik@pythonware.com
40 # http://www.pythonware.com
42 # --------------------------------------------------------------------
43 # The ElementTree toolkit is
45 # Copyright (c) 1999-2005 by Fredrik Lundh
47 # By obtaining, using, and/or copying this software and/or its
48 # associated documentation, you agree that you have read, understood,
49 # and will comply with the following terms and conditions:
51 # Permission to use, copy, modify, and distribute this software and
52 # its associated documentation for any purpose and without fee is
53 # hereby granted, provided that the above copyright notice appears in
54 # all copies, and that both that copyright notice and this permission
55 # notice appear in supporting documentation, and that the name of
56 # Secret Labs AB or the author not be used in advertising or publicity
57 # pertaining to distribution of the software without specific, written
60 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
61 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
62 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
63 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
64 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
65 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
66 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
68 # --------------------------------------------------------------------
74 "Element", "ElementTree",
76 "iselement", "iterparse",
78 "PI", "ProcessingInstruction",
88 # The <b>Element</b> type is a flexible container object, designed to
89 # store hierarchical data structures in memory. The type can be
90 # described as a cross between a list and a dictionary.
92 # Each element has a number of properties associated with it:
94 # <li>a <i>tag</i>. This is a string identifying what kind of data
95 # this element represents (the element type, in other words).</li>
96 # <li>a number of <i>attributes</i>, stored in a Python dictionary.</li>
97 # <li>a <i>text</i> string.</li>
98 # <li>an optional <i>tail</i> string.</li>
99 # <li>a number of <i>child elements</i>, stored in a Python sequence</li>
102 # To create an element instance, use the {@link #Element} or {@link
103 # #SubElement} factory functions.
105 # The {@link #ElementTree} class can be used to wrap an element
106 # structure, and convert it from and to XML.
109 import string, sys, re, platform
111 class _SimpleElementPath:
112 # emulate pre-1.2 find/findtext/findall behaviour
113 def find(self, element, tag):
118 def findtext(self, element, tag, default=None):
121 return elem.text or ""
123 def findall(self, element, tag):
125 return element.getiterator(tag[3:])
135 # FIXME: issue warning in this case?
136 ElementPath = _SimpleElementPath()
138 # TODO: add support for custom namespace resolvers/default namespaces
139 # TODO: add improved support for incremental parsing
144 # Internal element class. This class defines the Element interface,
145 # and provides a reference implementation of this interface.
147 # You should not create instances of this class directly. Use the
148 # appropriate factory functions instead, such as {@link #Element}
149 # and {@link #SubElement}.
154 # @see ProcessingInstruction
156 class _ElementInterface:
157 # <tag attrib>text<child/>...</tag>tail
160 # (Attribute) Element tag.
165 # (Attribute) Element attribute dictionary. Where possible, use
166 # {@link #_ElementInterface.get},
167 # {@link #_ElementInterface.set},
168 # {@link #_ElementInterface.keys}, and
169 # {@link #_ElementInterface.items} to access
170 # element attributes.
175 # (Attribute) Text before first subelement. This is either a
176 # string or the value None, if there was no text.
181 # (Attribute) Text after this element's end tag, but before the
182 # next sibling element's start tag. This is either a string or
183 # the value None, if there was no text.
185 tail = None # text after end tag, if any
187 def __init__(self, tag, attrib):
193 return "<Element %s at %x>" % (self.tag, id(self))
196 # Creates a new element object of the same type as this element.
198 # @param tag Element tag.
199 # @param attrib Element attributes, given as a dictionary.
200 # @return A new element instance.
202 def makeelement(self, tag, attrib):
203 return Element(tag, attrib)
206 # Returns the number of subelements.
208 # @return The number of subelements.
211 return len(self._children)
214 # Returns the given subelement.
216 # @param index What subelement to return.
217 # @return The given subelement.
218 # @exception IndexError If the given element does not exist.
220 def __getitem__(self, index):
221 return self._children[index]
224 # Replaces the given subelement.
226 # @param index What subelement to replace.
227 # @param element The new element value.
228 # @exception IndexError If the given element does not exist.
229 # @exception AssertionError If element is not a valid object.
231 def __setitem__(self, index, element):
232 assert iselement(element)
233 self._children[index] = element
236 # Deletes the given subelement.
238 # @param index What subelement to delete.
239 # @exception IndexError If the given element does not exist.
241 def __delitem__(self, index):
242 del self._children[index]
245 # Returns a list containing subelements in the given range.
247 # @param start The first subelement to return.
248 # @param stop The first subelement that shouldn't be returned.
249 # @return A sequence object containing subelements.
251 def __getslice__(self, start, stop):
252 return self._children[start:stop]
255 # Replaces a number of subelements with elements from a sequence.
257 # @param start The first subelement to replace.
258 # @param stop The first subelement that shouldn't be replaced.
259 # @param elements A sequence object with zero or more elements.
260 # @exception AssertionError If a sequence member is not a valid object.
262 def __setslice__(self, start, stop, elements):
263 for element in elements:
264 assert iselement(element)
265 self._children[start:stop] = list(elements)
268 # Deletes a number of subelements.
270 # @param start The first subelement to delete.
271 # @param stop The first subelement to leave in there.
273 def __delslice__(self, start, stop):
274 del self._children[start:stop]
277 # Adds a subelement to the end of this element.
279 # @param element The element to add.
280 # @exception AssertionError If a sequence member is not a valid object.
282 def append(self, element):
283 assert iselement(element)
284 self._children.append(element)
287 # Inserts a subelement at the given position in this element.
289 # @param index Where to insert the new subelement.
290 # @exception AssertionError If the element is not a valid object.
292 def insert(self, index, element):
293 assert iselement(element)
294 self._children.insert(index, element)
297 # Removes a matching subelement. Unlike the <b>find</b> methods,
298 # this method compares elements based on identity, not on tag
301 # @param element What element to remove.
302 # @exception ValueError If a matching element could not be found.
303 # @exception AssertionError If the element is not a valid object.
305 def remove(self, element):
306 assert iselement(element)
307 self._children.remove(element)
310 # Returns all subelements. The elements are returned in document
313 # @return A list of subelements.
314 # @defreturn list of Element instances
316 def getchildren(self):
317 return self._children
320 # Finds the first matching subelement, by tag name or path.
322 # @param path What element to look for.
323 # @return The first matching element, or None if no element was found.
324 # @defreturn Element or None
326 def find(self, path):
327 if ElementPath.find(self, path) == None:
328 return ElementPath.find(self, path.encode())
329 return ElementPath.find(self, path)
332 # Finds text for the first matching subelement, by tag name or path.
334 # @param path What element to look for.
335 # @param default What to return if the element was not found.
336 # @return The text content of the first matching element, or the
337 # default value no element was found. Note that if the element
338 # has is found, but has no text content, this method returns an
342 def findtext(self, path, default=None):
343 return ElementPath.findtext(self, path, default)
346 # Finds all matching subelements, by tag name or path.
348 # @param path What element to look for.
349 # @return A list or iterator containing all matching elements,
351 # @defreturn list of Element instances
353 def findall(self, path):
354 return ElementPath.findall(self, path)
357 # Resets an element. This function removes all subelements, clears
358 # all attributes, and sets the text and tail attributes to None.
363 self.text = self.tail = None
366 # Gets an element attribute.
368 # @param key What attribute to look for.
369 # @param default What to return if the attribute was not found.
370 # @return The attribute value, or the default value, if the
371 # attribute was not found.
372 # @defreturn string or None
374 def get(self, key, default=None):
375 res = self.attrib.get(key, default)
377 res = self.attrib.get(key.encode(), default)
378 if isinstance(res, bytes):
384 # Sets an element attribute.
386 # @param key What attribute to set.
387 # @param value The attribute value.
389 def set(self, key, value):
390 self.attrib[key] = value
393 # Gets a list of attribute names. The names are returned in an
394 # arbitrary order (just like for an ordinary Python dictionary).
396 # @return A list of element attribute names.
397 # @defreturn list of strings
401 for key in self.attrib.keys():
402 if isinstance(key, bytes):
403 res.append(key.decode())
409 # Gets element attributes, as a sequence. The attributes are
410 # returned in an arbitrary order.
412 # @return A list of (name, value) tuples for all attributes.
413 # @defreturn list of (string, string) tuples
416 return self.attrib.items()
419 # Creates a tree iterator. The iterator loops over this element
420 # and all subelements, in document order, and returns all elements
421 # with a matching tag.
423 # If the tree structure is modified during iteration, the result
426 # @param tag What tags to look for (default is to return all elements).
427 # @return A list or iterator containing all the matching elements.
428 # @defreturn list or iterator
430 def getiterator(self, tag=None):
434 if tag is None or self.tag == tag:
436 for node in self._children:
437 nodes.extend(node.getiterator(tag))
441 _Element = _ElementInterface
444 # Element factory. This function returns an object implementing the
445 # standard Element interface. The exact class or type of that object
446 # is implementation dependent, but it will always be compatible with
447 # the {@link #_ElementInterface} class in this module.
449 # The element name, attribute names, and attribute values can be
450 # either 8-bit ASCII strings or Unicode strings.
452 # @param tag The element name.
453 # @param attrib An optional dictionary, containing element attributes.
454 # @param **extra Additional attributes, given as keyword arguments.
455 # @return An element instance.
458 def Element(tag, attrib={}, **extra):
459 attrib = attrib.copy()
461 return _ElementInterface(tag, attrib)
464 # Subelement factory. This function creates an element instance, and
465 # appends it to an existing element.
467 # The element name, attribute names, and attribute values can be
468 # either 8-bit ASCII strings or Unicode strings.
470 # @param parent The parent element.
471 # @param tag The subelement name.
472 # @param attrib An optional dictionary, containing element attributes.
473 # @param **extra Additional attributes, given as keyword arguments.
474 # @return An element instance.
477 def SubElement(parent, tag, attrib={}, **extra):
478 attrib = attrib.copy()
480 element = parent.makeelement(tag, attrib)
481 parent.append(element)
485 # Comment element factory. This factory function creates a special
486 # element that will be serialized as an XML comment.
488 # The comment string can be either an 8-bit ASCII string or a Unicode
491 # @param text A string containing the comment string.
492 # @return An element instance, representing a comment.
495 def Comment(text=None):
496 element = Element(Comment)
501 # PI element factory. This factory function creates a special element
502 # that will be serialized as an XML processing instruction.
504 # @param target A string containing the PI target.
505 # @param text A string containing the PI contents, if any.
506 # @return An element instance, representing a PI.
509 def ProcessingInstruction(target, text=None):
510 element = Element(ProcessingInstruction)
511 element.text = target
513 element.text = element.text + " " + text
516 PI = ProcessingInstruction
519 # QName wrapper. This can be used to wrap a QName attribute value, in
520 # order to get proper namespace handling on output.
522 # @param text A string containing the QName value, in the form {uri}local,
523 # or, if the tag argument is given, the URI part of a QName.
524 # @param tag Optional tag. If given, the first argument is interpreted as
525 # an URI, and this argument is interpreted as a local name.
526 # @return An opaque object, representing the QName.
529 def __init__(self, text_or_uri, tag=None):
531 text_or_uri = "{%s}%s" % (text_or_uri, tag)
532 self.text = text_or_uri
536 return hash(self.text)
537 def __cmp__(self, other):
538 if isinstance(other, QName):
539 return cmp(self.text, other.text)
540 return cmp(self.text, other)
543 # ElementTree wrapper class. This class represents an entire element
544 # hierarchy, and adds some extra support for serialization to and from
547 # @param element Optional root element.
548 # @keyparam file Optional file handle or name. If given, the
549 # tree is initialized with the contents of this XML file.
553 def __init__(self, element=None, file=None):
554 assert element is None or iselement(element)
555 self._root = element # first node
560 # Gets the root element for this tree.
562 # @return An element instance.
569 # Replaces the root element for this tree. This discards the
570 # current contents of the tree, and replaces it with the given
571 # element. Use with care.
573 # @param element An element instance.
575 def _setroot(self, element):
576 assert iselement(element)
580 # Loads an external XML document into this element tree.
582 # @param source A file name or file object.
583 # @param parser An optional parser instance. If not given, the
584 # standard {@link XMLTreeBuilder} parser is used.
585 # @return The document root element.
588 def parse(self, source, parser=None):
589 if not hasattr(source, "read"):
590 # OP 14/11/2017 Ajout de traces pour essayer de decouvrir le pb
591 # de remontee de log des tests
592 print "TRACES OP - ElementTree.py/ElementTree.parse() source = '#%s#'" %source
593 source = open(source, "rb")
595 parser = XMLTreeBuilder()
597 data = source.read(32768)
601 self._root = parser.close()
605 # Creates a tree iterator for the root element. The iterator loops
606 # over all elements in this tree, in document order.
608 # @param tag What tags to look for (default is to return all elements)
609 # @return An iterator.
610 # @defreturn iterator
612 def getiterator(self, tag=None):
613 assert self._root is not None
614 return self._root.getiterator(tag)
617 # Finds the first toplevel element with given tag.
618 # Same as getroot().find(path).
620 # @param path What element to look for.
621 # @return The first matching element, or None if no element was found.
622 # @defreturn Element or None
624 def find(self, path):
625 assert self._root is not None
628 return self._root.find(path)
631 # Finds the element text for the first toplevel element with given
632 # tag. Same as getroot().findtext(path).
634 # @param path What toplevel element to look for.
635 # @param default What to return if the element was not found.
636 # @return The text content of the first matching element, or the
637 # default value no element was found. Note that if the element
638 # has is found, but has no text content, this method returns an
642 def findtext(self, path, default=None):
643 assert self._root is not None
646 return self._root.findtext(path, default)
649 # Finds all toplevel elements with the given tag.
650 # Same as getroot().findall(path).
652 # @param path What element to look for.
653 # @return A list or iterator containing all matching elements,
655 # @defreturn list of Element instances
657 def findall(self, path):
658 assert self._root is not None
661 return self._root.findall(path)
664 # Writes the element tree to a file, as XML.
666 # @param file A file name, or a file object opened for writing.
667 # @param encoding Optional output encoding (default is US-ASCII).
669 def write(self, file, encoding="us-ascii"):
670 assert self._root is not None
671 if not hasattr(file, "write"):
672 file = open(file, "wb")
674 encoding = "us-ascii"
675 elif encoding != "utf-8" and encoding != "us-ascii":
676 file.write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
677 self._write(file, self._root, encoding, {})
679 def _write(self, file, node, encoding, namespaces, margin=0):
683 file.write("<!-- %s -->\n" % _escape_cdata(node.text, encoding))
684 elif tag is ProcessingInstruction:
685 file.write("<?%s?>\n" % _escape_cdata(node.text, encoding))
688 xmlns_items = [] # new namespaces in this scope
690 if isinstance(tag, QName) or tag[:1] == "{":
691 tag, xmlns = fixtag(tag, namespaces)
692 if xmlns: xmlns_items.append(xmlns)
694 _raise_serialization_error(tag)
695 file.write(' ' * margin)
696 file.write(_encode("<", encoding) + _encode(tag, encoding))
697 if items or xmlns_items:
698 items = sorted(items) # lexical order
701 if isinstance(k, QName) or k[:1] == "{":
702 k, xmlns = fixtag(k, namespaces)
703 if xmlns: xmlns_items.append(xmlns)
705 _raise_serialization_error(k)
707 if isinstance(v, QName):
708 v, xmlns = fixtag(v, namespaces)
709 if xmlns: xmlns_items.append(xmlns)
711 _raise_serialization_error(v)
712 file.write(" %s=\"%s\"" % (k,v))
713 for k, v in xmlns_items:
714 file.write(" %s=\"%s\"" % (k,v))
715 if node.text or len(node):
718 file.write(_escape_cdata(node.text, encoding))
719 if len(node) > 0: file.write("\n")
721 self._write(file, n, encoding, namespaces, margin + 2)
722 if len(node) > 0: file.write(' ' * margin)
723 file.write(_encode("</", encoding) + _encode(tag, encoding) + _encode(">\n", encoding))
726 for k, v in xmlns_items:
729 file.write(_escape_cdata(node.tail, encoding))
731 # --------------------------------------------------------------------
735 # Checks if an object appears to be a valid element object.
737 # @param An element instance.
738 # @return A true value if this is an element object.
741 def iselement(element):
742 # FIXME: not sure about this; might be a better idea to look
743 # for tag/attrib/text attributes
744 return isinstance(element, _ElementInterface) or hasattr(element, "tag")
747 # Writes an element tree or element structure to sys.stdout. This
748 # function should be used for debugging only.
750 # The exact output format is implementation dependent. In this
751 # version, it's written as an ordinary XML file.
753 # @param elem An element tree or an individual element.
757 if not isinstance(elem, ElementTree):
758 elem = ElementTree(elem)
759 elem.write(sys.stdout)
760 tail = elem.getroot().tail
761 if not tail or tail[-1] != "\n":
762 sys.stdout.write("\n")
764 def _encode(s, encoding):
766 return s.encode(encoding)
767 except AttributeError:
768 return s # 1.5.2: assume the string uses the right encoding
770 if sys.version[:3] == "1.5":
771 _escape = re.compile(r"[&<>\"\x80-\xff]+") # 1.5.2
773 _escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
783 # "well-known" namespace prefixes
784 "http://www.w3.org/XML/1998/namespace": "xml",
785 "http://www.w3.org/1999/xhtml": "html",
786 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
787 "http://schemas.xmlsoap.org/wsdl/": "wsdl",
790 def _raise_serialization_error(text):
792 "cannot serialize %r (type %s)" % (text, type(text).__name__)
795 def _encode_entity(text, pattern=_escape):
796 # map reserved and non-ascii characters to numerical entities
797 def escape_entities(m, map=_escape_map):
800 for char in m.group():
803 text = "&#%d;" % ord(char)
805 return string.join(out, "")
807 return _encode(pattern.sub(escape_entities, text), "ascii")
809 _raise_serialization_error(text)
812 # the following functions assume an ascii-compatible encoding
815 def _escape_cdata(text, encoding=None, replace=str.replace):
816 # escape character data
818 if platform.python_version()[0] == '2': # python 2.x.y
821 text = _encode(text, encoding)
823 return _encode_entity(text)
825 text = replace(text, "&", "&")
826 text = replace(text, "<", "<")
827 text = replace(text, ">", ">")
828 text = replace(text, "####newLine####", "<br \>")
831 text = _encode(text, encoding)
833 return _encode_entity(text)
835 except (TypeError, AttributeError):
836 _raise_serialization_error(text)
838 def _escape_attrib(text, encoding=None, replace=str.replace):
839 # escape attribute value
841 text = replace(text, "&", "&")
842 text = replace(text, "'", "'") # FIXME: overkill
843 text = replace(text, "\"", """)
844 text = replace(text, "<", "<")
845 text = replace(text, ">", ">")
848 text = _encode(text, encoding)
850 return _encode_entity(text)
852 except (TypeError, AttributeError):
853 _raise_serialization_error(text)
855 def fixtag(tag, namespaces):
856 # given a decorated tag (of the form {uri}tag), return prefixed
857 # tag and namespace declaration, if any
858 if isinstance(tag, QName):
860 namespace_uri, tag = string.split(tag[1:], "}", 1)
861 prefix = namespaces.get(namespace_uri)
863 prefix = _namespace_map.get(namespace_uri)
865 prefix = "ns%d" % len(namespaces)
866 namespaces[namespace_uri] = prefix
870 xmlns = ("xmlns:%s" % prefix, namespace_uri)
873 return "%s:%s" % (prefix, tag), xmlns
876 # Parses an XML document into an element tree.
878 # @param source A filename or file object containing XML data.
879 # @param parser An optional parser instance. If not given, the
880 # standard {@link XMLTreeBuilder} parser is used.
881 # @return An ElementTree instance
883 def parse(source, parser=None):
884 # OP 14/11/2017 Ajout de traces pour essayer de decouvrir le pb
885 # de remontee de log des tests
886 print "TRACES OP - ElementTree.py/parse() source = '#%s#'" %source
888 tree.parse(source, parser)
892 # Parses an XML document into an element tree incrementally, and reports
893 # what's going on to the user.
895 # @param source A filename or file object containing XML data.
896 # @param events A list of events to report back. If omitted, only "end"
897 # events are reported.
898 # @return A (event, elem) iterator.
902 def __init__(self, source, events=None):
903 if not hasattr(source, "read"):
905 print "iterparse.__init__ source = %s" %source
906 source = open(source, "rb")
910 self.root = self._root = None
911 self._parser = XMLTreeBuilder()
912 # wire up the parser for event reporting
913 parser = self._parser._parser
914 append = self._events.append
920 parser.ordered_attributes = 1
921 parser.specified_attributes = 1
922 def handler(tag, attrib_in, event=event, append=append,
923 start=self._parser._start_list):
924 append((event, start(tag, attrib_in)))
925 parser.StartElementHandler = handler
926 except AttributeError:
927 def handler(tag, attrib_in, event=event, append=append,
928 start=self._parser._start):
929 append((event, start(tag, attrib_in)))
930 parser.StartElementHandler = handler
932 def handler(tag, event=event, append=append,
933 end=self._parser._end):
934 append((event, end(tag)))
935 parser.EndElementHandler = handler
936 elif event == "start-ns":
937 def handler(prefix, uri, event=event, append=append):
939 uri = _encode(uri, "ascii")
942 append((event, (prefix or "", uri)))
943 parser.StartNamespaceDeclHandler = handler
944 elif event == "end-ns":
945 def handler(prefix, event=event, append=append):
946 append((event, None))
947 parser.EndNamespaceDeclHandler = handler
952 item = self._events[self._index]
954 if self._parser is None:
955 self.root = self._root
963 data = self._file.read(16384)
965 self._parser.feed(data)
967 self._root = self._parser.close()
970 self._index = self._index + 1
978 def __getitem__(self, index):
982 # Parses an XML document from a string constant. This function can
983 # be used to embed "XML literals" in Python code.
985 # @param source A string containing XML data.
986 # @return An Element instance.
990 parser = XMLTreeBuilder()
992 return parser.close()
995 # Parses an XML document from a string constant, and also returns
996 # a dictionary which maps from element id:s to elements.
998 # @param source A string containing XML data.
999 # @return A tuple containing an Element instance and a dictionary.
1000 # @defreturn (Element, dictionary)
1003 parser = XMLTreeBuilder()
1005 tree = parser.close()
1007 for elem in tree.getiterator():
1014 # Parses an XML document from a string constant. Same as {@link #XML}.
1016 # @def fromstring(text)
1017 # @param source A string containing XML data.
1018 # @return An Element instance.
1019 # @defreturn Element
1024 # Generates a string representation of an XML element, including all
1027 # @param element An Element instance.
1028 # @return An encoded string containing the XML data.
1031 def tostring(element, encoding=None):
1036 file.write = data.append
1037 ElementTree(element).write(file, encoding)
1040 if isinstance(item, bytes):
1041 item = item.decode()
1043 return "".join(data2)
1046 # Generic element structure builder. This builder converts a sequence
1047 # of {@link #TreeBuilder.start}, {@link #TreeBuilder.data}, and {@link
1048 # #TreeBuilder.end} method calls to a well-formed element structure.
1050 # You can use this class to build an element structure using a custom XML
1051 # parser, or a parser for some other XML-like format.
1053 # @param element_factory Optional element factory. This factory
1054 # is called to create new Element instances, as necessary.
1058 def __init__(self, element_factory=None):
1059 self._data = [] # data collector
1060 self._elem = [] # element stack
1061 self._last = None # last element
1062 self._tail = None # true if we're after an end tag
1063 if element_factory is None:
1064 element_factory = _ElementInterface
1065 self._factory = element_factory
1068 # Flushes the parser buffers, and returns the toplevel documen
1071 # @return An Element instance.
1072 # @defreturn Element
1075 assert len(self._elem) == 0, "missing end tags"
1076 assert self._last != None, "missing toplevel element"
1081 if self._last is not None:
1083 for item in self._data:
1087 text += item.decode()
1089 assert self._last.tail is None, "internal error (tail)"
1090 self._last.tail = text
1092 assert self._last.text is None, "internal error (text)"
1093 self._last.text = text
1097 # Adds text to the current element.
1099 # @param data A string. This should be either an 8-bit string
1100 # containing ASCII text, or a Unicode string.
1102 def data(self, data):
1103 self._data.append(data)
1106 # Opens a new element.
1108 # @param tag The element name.
1109 # @param attrib A dictionary containing element attributes.
1110 # @return The opened element.
1111 # @defreturn Element
1113 def start(self, tag, attrs):
1115 self._last = elem = self._factory(tag, attrs)
1117 self._elem[-1].append(elem)
1118 self._elem.append(elem)
1123 # Closes the current element.
1125 # @param tag The element name.
1126 # @return The closed element.
1127 # @defreturn Element
1131 self._last = self._elem.pop()
1132 assert self._last.tag == tag,\
1133 "end tag mismatch (expected %s, got %s)" % (
1134 self._last.tag, tag)
1139 # Element structure builder for XML source data, based on the
1140 # <b>expat</b> parser.
1142 # @keyparam target Target object. If omitted, the builder uses an
1143 # instance of the standard {@link #TreeBuilder} class.
1144 # @keyparam html Predefine HTML entities. This flag is not supported
1145 # by the current implementation.
1149 class XMLTreeBuilder:
1151 def __init__(self, html=0, target=None):
1153 from xml.parsers import expat
1156 "No module named expat; use SimpleXMLTreeBuilder instead"
1158 self._parser = parser = expat.ParserCreate(None, "}")
1160 target = TreeBuilder()
1161 self._target = target
1162 self._names = {} # name memo cache
1164 parser.DefaultHandlerExpand = self._default
1165 parser.StartElementHandler = self._start
1166 parser.EndElementHandler = self._end
1167 parser.CharacterDataHandler = self._data
1168 # let expat do the buffering, if supported
1170 self._parser.buffer_text = 1
1171 except AttributeError:
1173 # use new-style attribute handling, if supported
1175 self._parser.ordered_attributes = 1
1176 self._parser.specified_attributes = 1
1177 parser.StartElementHandler = self._start_list
1178 except AttributeError:
1181 #if not parser.returns_unicode:
1182 # encoding = "utf-8"
1183 # target.xml(encoding, None)
1184 self._doctype = None
1187 def _fixtext(self, text):
1188 # convert text string to ascii, if possible
1190 return _encode(text, "ascii")
1191 except UnicodeError:
1194 def _fixname(self, key):
1195 # expand qname, and convert name string to ascii, if possible
1197 name = self._names[key]
1202 self._names[key] = name = self._fixtext(name)
1205 def _start(self, tag, attrib_in):
1206 fixname = self._fixname
1209 for key, value in attrib_in.items():
1210 attrib[fixname(key)] = self._fixtext(value)
1211 return self._target.start(tag, attrib)
1213 def _start_list(self, tag, attrib_in):
1214 fixname = self._fixname
1218 for i in range(0, len(attrib_in), 2):
1219 attrib[fixname(attrib_in[i])] = self._fixtext(attrib_in[i+1])
1220 return self._target.start(tag, attrib)
1222 def _data(self, text):
1223 return self._target.data(self._fixtext(text))
1225 def _end(self, tag):
1226 return self._target.end(self._fixname(tag))
1228 def _default(self, text):
1231 # deal with undefined entities
1233 self._target.data(self.entity[text[1:-1]])
1235 from xml.parsers import expat
1237 "undefined entity %s: line %d, column %d" %
1238 (text, self._parser.ErrorLineNumber,
1239 self._parser.ErrorColumnNumber)
1241 elif prefix == "<" and text[:9] == "<!DOCTYPE":
1242 self._doctype = [] # inside a doctype declaration
1243 elif self._doctype is not None:
1244 # parse doctype contents
1246 self._doctype = None
1248 text = string.strip(text)
1251 self._doctype.append(text)
1252 n = len(self._doctype)
1254 type = self._doctype[1]
1255 if type == "PUBLIC" and n == 4:
1256 name, type, pubid, system = self._doctype
1257 elif type == "SYSTEM" and n == 3:
1258 name, type, system = self._doctype
1264 self.doctype(name, pubid, system[1:-1])
1265 self._doctype = None
1268 # Handles a doctype declaration.
1270 # @param name Doctype name.
1271 # @param pubid Public identifier.
1272 # @param system System identifier.
1274 def doctype(self, name, pubid, system):
1278 # Feeds data to the parser.
1280 # @param data Encoded data.
1282 def feed(self, data):
1283 # OP 14/11/2017 Ajout de traces pour essayer de decouvrir le pb
1284 # de remontee de log des tests
1285 #print "TRACES OP - ElementTree.py/XMLTreeBuilder.feed() data = '#%s#'" %data
1286 self._parser.Parse(data, 0)
1289 # Finishes feeding data to the parser.
1291 # @return An element structure.
1292 # @defreturn Element
1295 self._parser.Parse("", 1) # end of data
1296 tree = self._target.close()
1297 del self._target, self._parser # get rid of circular references