def runTest(self): '''Test ParseTreeBuilder class''' # - Test \n before and after h / p / pre # - Test break line into lines input = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo<h level="1">bar</h>baz dus<pre>ja</pre>hmm <h level="2">foo </h>bar dus ja <emphasis>hmm dus ja </emphasis>grrr <strong>foo bar </strong> <strike></strike><emphasis> </emphasis>. </zim-tree>''' wanted = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo <h level="1">bar</h> baz dus <pre>ja </pre> hmm <h level="2">foo</h> bar dus ja <emphasis>hmm</emphasis> <emphasis>dus ja</emphasis> grrr <strong>foo</strong> <strong>bar</strong> . </zim-tree>''' # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder(target=ParseTreeBuilder()) builder.feed(input) root = builder.close() tree = ParseTree(root) self.assertEqualDiff(tree.tostring(), wanted)
def parse_xml(request, user): SoftLine.objects.filter(user=user.id).delete() # SoftLine.objects.all().delete() # print(request.POST) loader = InstalledSoftwareLoader() parser = XMLTreeBuilder(target=loader) parser.feed(request) # with open(file_name, 'rt') as f: # for line in f: # parser.feed(line) parser.close() for item in loader.items: line = SoftLine.objects.create(name=item.name, version=item.version, user=user) # print("Item => '%s'\n" % item.name) return len(loader.items)
def get_tree_from_xml(xml): # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder() builder.feed(xml) root = builder.close() return ParseTree(root)
def parse_xml(self, raw_xml, parser=None): if not parser: parser = XMLTreeBuilder() parser.feed(raw_xml) self._root = parser.close() return self._root
def new_parsetree_from_xml(xml): # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder from zim.formats import ParseTree builder = XMLTreeBuilder() builder.feed(xml) root = builder.close() return ParseTree(root)
def runTest(self): '''Test OldParseTreeBuilder class''' # - Test \n before and after h / p / pre # - Test break line into lines input = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo<h level="1">bar</h>baz dus<pre>ja</pre>hmm <h level="2">foo </h>bar dus ja <emphasis>hmm dus ja </emphasis>grrr <strong>foo bar </strong> <strike></strike><emphasis> </emphasis>. </zim-tree>''' wanted = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo <h level="1">bar</h> baz dus <pre>ja </pre>hmm <h level="2">foo</h> bar dus ja <emphasis>hmm</emphasis> <emphasis>dus ja</emphasis> grrr <strong>foo</strong> <strong>bar</strong> . </zim-tree>''' # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder(target=OldParseTreeBuilder()) builder.feed(input) root = builder.close() tree = ParseTree(root) self.assertEqual(tree.tostring(), wanted)
def read_generated_namelist(nml_file=None): """ This will read a set of previously generated namelists from a file. """ if not nml_file: # No file specified return None else: read_dict = {} target = InputXMLToNamelist(read_dict) parser = XMLTreeBuilder(target=target) with open(nml_file,'rt') as f: for line in f: parser.feed(line) parser.close() return read_dict
def kplist_parse(plist): """Parse a kernel-style property list.""" try: builder = _KPlistBuilder() parser = XMLTreeBuilder(target=builder) parser.feed(plist) return parser.close() except: return None
def get_unread_count(self): url = self.urls['reader'] + self.urls['unread-count'] data = self.get_results(url) if data is None: return tree = XMLTreeBuilder() tree.feed(data) root_object = tree.close() li = root_object.getchildren()[1] counts = [] for obj in li: feed_id, count, timestamp = \ [ele.text for ele in obj.getchildren()] counts.append((feed_id, count, timestamp)) return counts
def get_subscriptions(self): url = self.urls['reader'] + self.urls['subscription-list'] data = self.get_results(url) if data is None: return tree = XMLTreeBuilder() tree.feed(data) root_object = tree.close() li = root_object.getchildren()[0] feeds = {} for obj in li: feed_id = obj.getchildren()[0].text title = obj.getchildren()[1].text feeds[feed_id] = title return feeds
def parseXRDS(text): """Parse the given text as an XRDS document. @return: ElementTree containing an XRDS document @raises XRDSError: When there is a parse error or the document does not contain an XRDS. """ try: parser = XMLTreeBuilder() parser.feed(text) element = parser.close() except XMLError, why: exc = XRDSError('Error parsing document as XML') exc.reason = why raise exc
def _set_led(self, val): global glob_led_policy, glob_led_signal_list if not self.preview_list[val].display_type == 'not_checked': return new_signal = ElementTree() parser = XMLTreeBuilder() if val == 3: parser.feed(glob_signal_ALL) else: parser.feed(glob_signal_RBO) new_signal._root = parser.close() new_signal_list = new_signal.getiterator("signal") new_signal_list[0].attrib = glob_led_signal_list[ self.signal_num].attrib new_signal_list[0].tail = glob_led_signal_list[self.signal_num].tail glob_led_policy.remove(glob_led_signal_list[self.signal_num]) glob_led_policy.insert(self.signal_num, new_signal_list[0]) glob_led_signal_list[self.signal_num] = new_signal_list[0] for i in range(len(self.preview_list)): self.preview_list[i].display_type = self._get_led(i) self.preview_list[i].reset_view()
def body(self, xml): """ Body importer """ if isinstance(xml, (str, unicode)): parser = XMLTreeBuilder() parser.feed(xml) tree = parser.close() tree = ElementTree(tree) elem = tree.getroot() else: elem = xml if elem.tag != 'object': raise AttributeError('Invalid xml root element %s' % elem.tag) name = elem.get('name') if not name: raise AttributeError('No name provided for object') if hasattr(self.context, '__name__') and ( name != self.context.__name__): raise AttributeError(('XML root object name %s ' 'should match context name %s') % ( name, self.context.__name__)) for child in elem.getchildren(): if child.tag == 'property': self.attribute = child elif child.tag == 'object': self.child = child wftool = getToolByName(self.context, 'portal_workflow') state = wftool.getInfoFor(self.context, 'review_state', '(Unknown)') if state != 'published': try: wftool.doActionFor(self.context, 'publish', comment='Initial import') except Exception, err: logger.exception(err)
from xml.etree.ElementTree import XMLTreeBuilder class MaxDepth: # The target object of the parser maxDepth = 0 depth = 0 def start(self, tag, attrib): # Called for each opening tag. self.depth += 1 if self.depth > self.maxDepth: self.maxDepth = self.depth def end(self, tag): # Called for each closing tag. self.depth -= 1 def data(self, data): pass # We do not need to do anything with data. def close(self): # Called when all data has been parsed. return self.maxDepth target = MaxDepth() parser = XMLTreeBuilder(target=target) n = int(raw_input()) exampleXml = "" for _ in range(n): exampleXml += raw_input()+"\n" parser.feed(exampleXml) print parser.close()-1
def start(self, tag, attrib): if tag != "outline": # Ignora qualsiasi al di fuori di outline return if not attrib.get("xmlUrl"): # Ricorda il gruppo corrente self.group_name = attrib["text"] else: # Scrive una voce di podcast self.writer.writerow((self.group_name, attrib["text"], attrib["xmlUrl"], attrib.get("htmlUrl", ""))) def end(self, tag): # Ignora i tag di chiusura pass def data(self, data): # Ignora i dati all'interno dei nodi pass def close(self): # Nulla di speciale da fare qui return target = PodcastListToCSV(sys.stdout) parser = XMLTreeBuilder(target=target) with open("podcasts.opml", "rt") as f: for line in f: parser.feed(line) parser.close()
def parse(self, source, parser=None): if not parser: parser = XMLTreeBuilder() parser.feed(source) self._root = parser.close() return self._root
# Remember the current group self.group_name = attrib['text'] else: # Output a podcast entry self.writer.writerow(( self.group_name, attrib['text'], attrib['xmlUrl'], attrib.get('htmlUrl', ''), )) def end(self, tag): # Ignore closing tags pass def data(self, data): # Ignore data inside nodes pass def close(self): # Nothing special to do here return target = PodcastListToCSV(sys.stdout) parser = XMLTreeBuilder(target=target) with open('podcasts.opml', 'rt') as f: for line in f: parser.feed(line) parser.close()