我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用xml.etree.ElementTree.TreeBuilder()。
def __init__(self, quiet=False): if sys.flags.optimize >= 2: # under -OO, doctests cannot be run and therefore not all warnings # will be emitted quiet = True deprecations = ( # Search behaviour is broken if search path starts with "/". ("This search is broken in 1.3 and earlier, and will be fixed " "in a future version. If you rely on the current behaviour, " "change it to '.+'", FutureWarning), # Element.getchildren() and Element.getiterator() are deprecated. ("This method will be removed in future versions. " "Use .+ instead.", DeprecationWarning), ("This method will be removed in future versions. " "Use .+ instead.", PendingDeprecationWarning), # XMLParser.doctype() is deprecated. ("This method of XMLParser is deprecated. Define doctype.. " "method on the TreeBuilder target.", DeprecationWarning)) self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet)
def xmltoolkit63(): """ Check reference leak. >>> xmltoolkit63() >>> count = sys.getrefcount(None) #doctest: +SKIP >>> for i in range(1000): ... xmltoolkit63() >>> sys.getrefcount(None) - count #doctest: +SKIP 0 """ tree = ET.TreeBuilder() tree.start("tag", {}) tree.data("text") tree.end("tag") # --------------------------------------------------------------------
def xmltoolkit63(): """ Check reference leak. >>> xmltoolkit63() >>> count = sys.getrefcount(None) >>> for i in range(1000): ... xmltoolkit63() >>> sys.getrefcount(None) - count 0 """ tree = ET.TreeBuilder() tree.start("tag", {}) tree.data("text") tree.end("tag") # --------------------------------------------------------------------
def __init__(self, quiet=False): if sys.flags.optimize >= 2: # under -OO, doctests cannot be run and therefore not all warnings # will be emitted quiet = True deprecations = ( # Search behaviour is broken if search path starts with "/". ("This search is broken in 1.3 and earlier, and will be fixed " "in a future version. If you rely on the current behaviour, " "change it to '.+'", FutureWarning), # Element.getchildren() and Element.getiterator() are deprecated. ("This method will be removed in future versions. " "Use .+ instead.", DeprecationWarning), ("This method will be removed in future versions. " "Use .+ instead.", PendingDeprecationWarning), # XMLParser.doctype() is deprecated. ("This method of XMLParser is deprecated. Define doctype.. " "method on the TreeBuilder target.", DeprecationWarning)) self.checkwarnings = test_support.check_warnings(*deprecations, quiet=quiet)
def filter(self, text): wanted_status = ['created_at', 'id', 'text', 'source', 'favorited'] wanted_user = ['id', 'name', 'screen_name', 'profile_image_url', 'url'] root = ET.fromstring(text) builder = ET.TreeBuilder() builder.start('statuses', {'type': 'array'}) for status in root.findall('status'): builder.start('status', {}) for tag in wanted_status: copy_element(builder, status, tag) user = status.find('user') builder.start('user', {}) for tag in wanted_user: copy_element(builder, user, tag) builder.end('user') builder.end('status') builder.end('statuses') return ET.tostring(builder.close())
def filter(self, text): wanted_status = ['created_at', 'id', 'text', 'source', 'favorited'] wanted_user = ['id', 'name', 'screen_name', 'url'] root = ET.fromstring(text) builder = ET.TreeBuilder() builder.start('statuses', {'type': 'array'}) for status in root.findall('status'): builder.start('status', {}) for tag in wanted_status: copy_element(builder, status, tag) user = status.find('user') builder.start('user', {}) for tag in wanted_user: copy_element(builder, user, tag) builder.end('user') builder.end('status') builder.end('statuses') return ET.tostring(builder.close(), 'UTF-8')
def filter(self, text): wanted_dm = ['created_at', 'id', 'text', 'source', 'sender_id', 'recipient_id', 'sender_screen_name', 'recipient_screen_name'] root = ET.fromstring(text) builder = ET.TreeBuilder() builder.start('direct-messages', {'type': 'array'}) for status in root.findall('direct_message'): builder.start('direct_message', {}) for tag in wanted_status: copy_element(builder, status, tag) sender = status.find('sender') builder.start('sender', {}) copy_element(builder, sender, 'profile_image_url') builder.end('sender') recipient = status.find('recipient') builder.start('recipient', {}) copy_element(builder, recipient, 'profile_image_url') builder.end('recipient') builder.end('direct_message') builder.end('direct-messages') return ET.tostring(builder.close())
def filter(self, text): wanted_dm = ['created_at', 'id', 'text', 'source', 'sender_id', 'recipient_id', 'sender_screen_name', 'recipient_screen_name'] root = ET.fromstring(text) dm = root.find('direct_message') builder = ET.TreeBuilder() builder.start('direct_message', {}) for tag in wanted_dm: copy_element(builder, dm, tag) sender = status.find('sender') builder.start('sender', {}) copy_element(builder, sender, 'profile_image_url') builder.end('sender') recipient = status.find('recipient') builder.start('recipient', {}) copy_element(builder, recipient, 'profile_image_url') builder.end('recipient') builder.end('direct_message') return ET.tostring(builder.close())
def bug_1534630(): """ >>> bob = ET.TreeBuilder() >>> e = bob.data("data") >>> e = bob.start("tag", {}) >>> e = bob.end("tag") >>> e = bob.close() >>> serialize(e) '<tag />' """
def parse(self, encoding=None, errors='strict', **kwargs): """ Return the contents of toolbox settings file with a nested structure. :param encoding: encoding used by settings file :type encoding: str :param errors: Error handling scheme for codec. Same as ``decode()`` builtin method. :type errors: str :param kwargs: Keyword arguments passed to ``StandardFormat.fields()`` :type kwargs: dict :rtype: ElementTree._ElementInterface """ builder = TreeBuilder() for mkr, value in self.fields(encoding=encoding, errors=errors, **kwargs): # Check whether the first char of the field marker # indicates a block start (+) or end (-) block=mkr[0] if block in ("+", "-"): mkr=mkr[1:] else: block=None # Build tree on the basis of block char if block == "+": builder.start(mkr, {}) builder.data(value) elif block == '-': builder.end(mkr) else: builder.start(mkr, {}) builder.data(value) builder.end(mkr) return builder.close()
def __init__(self, encoding=None): self.__stack = [] self.__builder = ET.TreeBuilder() self.encoding = encoding or "iso-8859-1" HTMLParser.__init__(self) ## # Flushes parser buffers, and return the root element. # # @return An Element instance.
def filter(self, text): wanted_dm = ['created_at', 'id', 'text', 'source', 'sender_id', 'recipient_id', 'sender_screen_name', 'recipient_screen_name'] root = ET.fromstring(text) builder = ET.TreeBuilder() builder.start('direct-messages', {'type': 'array'}) for dm in root.findall('direct_message'): builder.start('direct_message', {}) for tag in wanted_dm: copy_element(builder, dm, tag) builder.end('direct_message') builder.end('direct-messages') return ET.tostring(builder.close())
def __init__(self): et.TreeBuilder.__init__(self) self.root_element = None self.root_element_closed = False
def start(self, tag, attrs): element = et.TreeBuilder.start(self, tag, attrs) if self.root_element is None: self.root_element = element return element
def end(self, tag): element = et.TreeBuilder.end(self, tag) if self.root_element is element: self.root_element_closed = True return element
def parsefile(): """ Test parsing from file. >>> tree = ET.parse(SIMPLE_XMLFILE) >>> normalize_crlf(tree) >>> tree.write(sys.stdout, encoding='unicode') <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> tree = ET.parse(SIMPLE_NS_XMLFILE) >>> normalize_crlf(tree) >>> tree.write(sys.stdout, encoding='unicode') <ns0:root xmlns:ns0="namespace"> <ns0:element key="value">text</ns0:element> <ns0:element>text</ns0:element>tail <ns0:empty-element /> </ns0:root> >>> with open(SIMPLE_XMLFILE) as f: ... data = f.read() >>> parser = ET.XMLParser() >>> parser.version # doctest: +ELLIPSIS 'Expat ...' >>> parser.feed(data) >>> print(serialize(parser.close())) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> parser = ET.XMLTreeBuilder() # 1.2 compatibility >>> parser.feed(data) >>> print(serialize(parser.close())) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> target = ET.TreeBuilder() >>> parser = ET.XMLParser(target=target) >>> parser.feed(data) >>> print(serialize(parser.close())) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> """
def parsefile(): """ Test parsing from file. >>> tree = ET.parse(SIMPLE_XMLFILE) >>> normalize_crlf(tree) >>> tree.write(sys.stdout) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> tree = ET.parse(SIMPLE_NS_XMLFILE) >>> normalize_crlf(tree) >>> tree.write(sys.stdout) <ns0:root xmlns:ns0="namespace"> <ns0:element key="value">text</ns0:element> <ns0:element>text</ns0:element>tail <ns0:empty-element /> </ns0:root> >>> with open(SIMPLE_XMLFILE) as f: ... data = f.read() >>> parser = ET.XMLParser() >>> parser.version # doctest: +ELLIPSIS 'Expat ...' >>> parser.feed(data) >>> print serialize(parser.close()) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> parser = ET.XMLTreeBuilder() # 1.2 compatibility >>> parser.feed(data) >>> print serialize(parser.close()) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> >>> target = ET.TreeBuilder() >>> parser = ET.XMLParser(target=target) >>> parser.feed(data) >>> print serialize(parser.close()) <root> <element key="value">text</element> <element>text</element>tail <empty-element /> </root> """