本文整理匯總了Python中xml.etree.ElementTree.XMLTreeBuilder類的典型用法代碼示例。如果您正苦於以下問題:Python XMLTreeBuilder類的具體用法?Python XMLTreeBuilder怎麽用?Python XMLTreeBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了XMLTreeBuilder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_tree_from_xml
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)
示例2: xmlmodel
class xmlmodel(object) :
def __init__(self, parent = None) :
self.data = {}
self.axes = {}
self.parent = parent
def parse_file(self, fname) :
if os.path.exist(fname) :
self.xml = XMLTreeBuilder(target=self)
self.xml.curr = self
fh = file(fname)
self.xml.feed(fh.read())
def set_key(self, key, value) :
if len(key) > 1 :
if not self.axes.has_key(key[0]) :
self.axes[key[0]] = xmlmodel(self)
self.axes[key[0]].set_key(key[1:], value)
else :
self.data[key[0]] = value
def get_key(self, key) :
if len(key) > 1 :
if not self.axes.has_key(key[0]) :
return None
return self.axes[key[0]].get_key(key[1:])
else :
return self.data.get(key[0])
def start(self, name, attrs) :
if name == "data" :
self.xml.currkey = attrs['key']
elif name == 'axis' :
newaxis = xmlmodel(self.xml.curr)
self.axes[attrs['key']] = newaxis
self.xml.curr = newaxis
def data(self, text) :
if self.xml.currkey :
self.xml.curr[self.xml.currkey] = text
def end(self, name) :
if name == "axis" and self.xml.curr.parent :
self.xml.curr = self.xml.curr.parent
def save(self, fname) :
fh = file(fname, "w")
fh.writelines(("<?xml version='1.0'?>\n",
"<palaso_model>\n"))
self.write_xml(fh, "")
fh.write("</palaso_model>\n")
def write_xml(self, fh, indent) :
indent += " "
for (k, v) in self.data.iteritems() :
fh.write("%s<data key='%s'>%s</data>\n" % (indent, k, v))
for (k, v) in self.axes.iteritems() :
fh.write("%s<axis key='%s'>\n" % (indent, k))
v.write_xml(fh, indent)
fh.write("%s</axis>\n" % (indent))
示例3: __init__
def __init__(self, target=None):
"""Initialize
@param target Target class
"""
from xml.etree.ElementTree import XMLTreeBuilder
XMLTreeBuilder.__init__(self, html=0, target=target)
self._parser.CommentHandler = self.comment
示例4: runTest
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)
示例5: parse_xml
def parse_xml(self, raw_xml, parser=None):
if not parser:
parser = XMLTreeBuilder()
parser.feed(raw_xml)
self._root = parser.close()
return self._root
示例6: __init__
def __init__(self, filePath, absUrl, xmlIf, treeWrapper):
XMLTreeBuilder.__init__(
self, target=TreeBuilder(
element_factory=ElementExtension))
self._parser.StartNamespaceDeclHandler = self._start_ns
self._parser.EndNamespaceDeclHandler = self._end_ns
self.namespaces = []
XmlIfBuilderExtensionBase.__init__(
self,
filePath,
absUrl,
treeWrapper,
xmlIf.elementWrapperClass)
示例7: get_unread_count
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
示例8: get_subscriptions
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
示例9: parse_xml
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)
示例10: parseXRDS
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
示例11: read_generated_namelist
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
示例12: csv_treebuilder
def csv_treebuilder():
import csv
from xml.etree.ElementTree import XMLTreeBuilder
import sys
class PodcastListToCSV(object):
def __init__(self, outputFile):
self.writer = csv.writer(outputFile, quoting=csv.QUOTE_NONNUMERIC)
self.group_name = ''
def start(self, tag, attrib):
if tag != 'outline':
# Ignore anything not part of the outline
return
if not attrib.get('xmlUrl'):
# 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):
return
target = PodcastListToCSV(sys.stdout)
parser = XMLTreeBuilder(target=target)
with open('podcasts.opml', 'rt') as f:
for line in f:
parser.feed(line)
示例13: body
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)
示例14: build_sql_queries
def build_sql_queries(filename, bbox=None):
if bbox != None:
target = SQLBuilder(box_node_list='box_node_list',
box_way_list='box_way_list')
else:
target = SQLBuilder()
init_queries = []
## If bounding box is given, create a temporary table and
## store all matching nodes there
if bbox != None:
## Create tables
init_queries.append("CREATE TEMPORARY TABLE box_node_list "
"(id bigint PRIMARY KEY) ON COMMIT DROP")
init_queries.append("CREATE TEMPORARY TABLE box_way_list "
"(id bigint PRIMARY KEY) ON COMMIT DROP")
## Fill them
bbox_sql = "GeomFromEWKT('POLYGON((%f %f,%f %f,%f %f,%f %f,%f %f))')"%\
(bbox[0],bbox[1],bbox[2],bbox[1],bbox[2],bbox[3],bbox[0],bbox[3],
bbox[0],bbox[1])
srid = "find_srid('','nodes','geom')"
init_queries.append("INSERT INTO box_node_list SELECT id FROM nodes "
"WHERE (geom && SetSRID(%s,%s) )"%(bbox_sql,srid))
init_queries.append("INSERT INTO box_way_list "
"SELECT wn.way_id FROM way_nodes wn "
"INNER JOIN box_node_list n ON wn.node_id = n.id "
"GROUP BY wn.way_id")
parser = XMLTreeBuilder(target=target)
parser.feed(open(filename).read())
return init_queries, target.result
示例15: start
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()