本文整理汇总了Python中xml.dom.pulldom.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: read
def read(self):
events = pulldom.parse(self.filename)
events = pulldom.parse(sys.argv[1])
for (event, node) in events:
if event == "START_ELEMENT" and node.tagName == "entry":
events.expandNode(node)
entry = Entry(node)
yield entry
开发者ID:NAzT,项目名称:Yaitron,代码行数:8,代码来源:yaitron_reader.py
示例2: test_parse
def test_parse(self):
"""Minimal test of DOMEventStream.parse()"""
# This just tests that parsing from a stream works. Actual parser
# semantics are tested using parseString with a more focused XML
# fragment.
# Test with a filename:
handler = pulldom.parse(tstfile)
self.addCleanup(handler.stream.close)
list(handler)
# Test with a file object:
with open(tstfile, "rb") as fin:
list(pulldom.parse(fin))
开发者ID:10sr,项目名称:cpython,代码行数:15,代码来源:test_pulldom.py
示例3: parseXML
def parseXML(stream, parser=None):
if isinstance(stream, six.string_types):
events = pulldom.parseString(stream, parser)
else:
events = pulldom.parse(stream, parser)
document = None
chain = []
for event, node in events:
if event == "START_DOCUMENT":
chain.append(XMLNode("DOCUMENT", {}))
elif event == "START_ELEMENT":
node = XMLNode.fromDOMNode(node)
if chain:
chain[-1].children.append(node)
chain.append(node)
elif event == "END_ELEMENT":
chain.pop(-1)
elif event == "CHARACTERS":
chain[-1].data += node.data
elif event == "END_DOCUMENT":
document = chain.pop(-1)
return document or chain[0]
开发者ID:astaric,项目名称:orange-bio,代码行数:27,代码来源:biomart.py
示例4: process_metadata
def process_metadata(f):
count = 0
metadata = {}
events = pulldom.parse(f)
for event, node in events:
if node.localName == 'gpxFile' and event == pulldom.START_ELEMENT:
m = {}
for k in ['visibility', 'user']:
if node.hasAttribute(k):
m[k] = node.getAttribute(k)
for k in ['id', 'uid', 'points']:
if node.hasAttribute(k):
m[k] = int(node.getAttribute(k))
if node.hasAttribute('timestamp'):
m['date'] = node.getAttribute('timestamp')[0:10]
events.expandNode(node)
desc = node.getElementsByTagName('description')
if desc and desc[0].firstChild:
m['description'] = desc[0].firstChild.data[0:500]
tags = node.getElementsByTagName('tag')
if tags:
t = []
for tag in tags:
if tag.firstChild:
t.append(tag.firstChild.data)
m['tags'] = t
metadata[node.getAttribute('filename')] = m
count += 1
if count % 10000 == 0:
sys.stdout.write('.')
sys.stdout.flush()
return metadata
开发者ID:Zverik,项目名称:gpx2pgsql,代码行数:32,代码来源:gpx2pgsql.py
示例5: parse
def parse(self, source, base_uri):
"""parse(source) : Pattern
Parses the XML from the input stream and returns a Pattern tree.
"""
self.base_uri = base_uri
stream = util.DOMTokenStream(pulldom.parse(source))
stream.set_legal_attributes(_legal_attributes)
self.stream = stream
# Process the document prologue
assert stream.get_next_event()[0] == pulldom.START_DOCUMENT
try:
event, node = stream.get_next_event()
except StopIteration:
raise RuntimeError, ("No first element found -- " "missing RELAX NG namespace declaration?")
assert is_start_of_pattern(event, node)
root_grammar = stream.root_grammar
root_grammar.add_start_sym("", self.parse_rest_of_pattern(event, node))
root_grammar.combine()
pattern = root_grammar.start_symbol
if pattern is relaxng.NotAllowed:
raise RuntimeError, "Schema reduces to NotAllowed (can never be valid)"
return relaxng.Schema(pattern)
开发者ID:janbrohl,项目名称:PyXML,代码行数:26,代码来源:fullparser.py
示例6: parse
def parse (source):
"""parse(source) : Pattern
Parses the XML from the input stream and returns a Pattern tree.
"""
stream = util.DOMTokenStream(pulldom.parse(source))
element_map = {}
# for item in stream:
# print item, stream.parents
# Process the document prologue and the first two elements
assert stream.next()[0] == pulldom.START_DOCUMENT
stream.expect_element('grammar')
stream.expect_element('start')
# Parse the main pattern body
pattern = parse_top(stream)
stream.expect_end_element('start')
# Process definition section
while 1:
event, node = stream.get_next_event()
if event == pulldom.END_ELEMENT and node.localName == 'grammar':
# We're done
break
elif event == pulldom.START_ELEMENT and node.localName == 'define':
# Parse definition
ncname = node.getAttributeNS(RNG.BASE, 'name')
stream.expect_element('element')
nc = parse_nameclass(stream)
pattern = parse_top(stream)
stream.expect_end_element('element')
stream.expect_end_element('define')
element_map[ncname] = relaxng.Element(nc=nc,
p1=pattern)
else:
raise RuntimeError, 'Unexpected event: %r, %r' % (event, node)
# Loop through all the patterns, replacing Ref instances
# with the corresponding Element instance
# XXX does this always terminate, given that there can be
# cycles of Elements?
# XXX on the other hand, does this cover every single pattern
# node that could contain a Ref instance?
queue = [pattern] + element_map.values()
while len(queue):
head = queue.pop()
if hasattr(head, 'p1'):
if isinstance(head.p1, Ref):
head.p1 = element_map[head.p1.ref_name]
else:
queue.append(head.p1)
if hasattr(head, 'p2'):
if isinstance(head.p2, Ref):
head.p2 = element_map[head.p2.ref_name]
else:
queue.append(head.p2)
return relaxng.Schema(pattern)
开发者ID:janbrohl,项目名称:PyXML,代码行数:60,代码来源:parser.py
示例7: sort_departs
def sort_departs(routefilename, outfile):
routes_doc = pulldom.parse(sys.argv[1])
vehicles = []
root = None
for event, parsenode in routes_doc:
if event == pulldom.START_ELEMENT:
if root is None:
root = parsenode.localName
outfile.write("<%s>\n" % root)
continue
routes_doc.expandNode(parsenode)
departAttr = DEPART_ATTRS.get(parsenode.localName)
if departAttr is not None:
start = float(parsenode.getAttribute(departAttr))
vehicles.append(
(start, parsenode.toprettyxml(indent="", newl="")))
else:
# copy to output
outfile.write(
" " * 4 + parsenode.toprettyxml(indent="", newl="") + "\n")
print('read %s elements.' % len(vehicles))
vehicles.sort(key=lambda v: v[0])
for depart, vehiclexml in vehicles:
outfile.write(" " * 4)
outfile.write(vehiclexml)
outfile.write("\n")
outfile.write("</%s>\n" % root)
print('wrote %s elements.' % len(vehicles))
开发者ID:aarongolliver,项目名称:sumo,代码行数:29,代码来源:sort_routes.py
示例8: call
def call(self):
"""
Makes a request to cghub server.
Returns generator that returns Result objects.
"""
self.patch_input_data()
query = self.build_query()
url = '%s%s' % (self.server_url, self.uri)
if query:
url = '%s?%s' % (url, query)
xml = self.get_source_file(url)
if self.format == self.FORMAT_JSON:
results = ijson.items(xml, 'response.docs.item')
for item in results:
yield item
else:
# http://docs.python.org/dev/library/xml.dom.pulldom.html
doc = pulldom.parse(xml)
for event, node in doc:
if event == pulldom.START_ELEMENT:
if node.tagName == 'doc':
doc.expandNode(node)
# convert to python object
# http://docs.python.org/2/library/xml.etree.elementtree.html
result_xml = node.toxml(encoding='utf-8')
tree = ElementTree.fromstring(result_xml)
result = Result(tree)
yield self.patch_result(result, result_xml)
elif node.tagName == 'result':
self.hits = int(node.getAttribute('numFound'))
开发者ID:42cc,项目名称:cghub-python-api,代码行数:30,代码来源:api.py
示例9: parse
def parse(xmlfile, element_names, element_attrs={}, attr_conversions={}):
"""
Parses the given element_names from xmlfile and yield compound objects for
their xml subtrees (no extra objects are returned if element_names appear in
the subtree) The compound objects provide all element attributes of
the root of the subtree as attributes unless attr_names are supplied. In this
case attr_names maps element names to a list of attributes which are
supplied. If attr_conversions is not empty it must map attribute names to
callables which will be called upon the attribute value before storing under
the attribute name.
The compound objects gives dictionary style access to list of compound
objects o for any children with the given element name
o['child_element_name'] = [osub0, osub1, ...]
As a shorthand, attribute style access to the list of child elements is
provided unless an attribute with the same name as the child elements
exists (i.e. o.child_element_name = [osub0, osub1, ...])
@Note: All elements with the same name must have the same type regardless of
the subtree in which they occur
@Note: Attribute names may be modified to avoid name clashes
with python keywords.
@Note: The element_names may be either a single string or a list of strings.
@Example: parse('plain.edg.xml', ['edge'])
"""
if isinstance(element_names, str):
element_names = [element_names]
elementTypes = {}
xml_doc = pulldom.parse(xmlfile)
for event, parsenode in xml_doc:
if event == pulldom.START_ELEMENT and parsenode.localName in element_names:
xml_doc.expandNode(parsenode)
yield _get_compound_object(parsenode, elementTypes,
parsenode.localName, element_attrs, attr_conversions)
开发者ID:aarongolliver,项目名称:sumo,代码行数:32,代码来源:__init__.py
示例10: handle_children
def handle_children(xmlfile, handle_parsenode):
root_open = None
root_close = None
level = 0
xml_doc = pulldom.parse(xmlfile)
for event, parsenode in xml_doc:
if event == pulldom.START_ELEMENT:
# print level, parsenode.getAttribute(ID_ATTR)
if level == 0:
root_open = parsenode.toprettyxml(indent="")
# since we did not expand root_open contains the closing slash
root_open = root_open[:-3] + ">\n"
# change the schema for edge diffs
root_open = root_open.replace(
"edges_file.xsd", "edgediff_file.xsd")
root_close = "</%s>\n" % parsenode.localName
if level == 1:
# consumes END_ELEMENT, no level increase
xml_doc.expandNode(parsenode)
handle_parsenode(parsenode)
else:
level += 1
elif event == pulldom.END_ELEMENT:
level -= 1
return root_open, root_close
开发者ID:planetsumo,项目名称:sumo,代码行数:25,代码来源:netdiff.py
示例11: PullParse
def PullParse(fileName, katalog, baseName):
db = XmlDB(katalog + "/" + baseName)
db.Open()
events = pulldom.parse(fileName)
pre = 1
stack = []
stack.append(XmlNodeTuple(0, type="elem", data="doc"))
for (event, node) in events:
if event == "START_ELEMENT":
stack.append(XmlNodeTuple(pre, type="elem", data=node.tagName))
pre += 1
if event == "END_ELEMENT":
x = stack.pop()
if len(stack) > 0:
parent = stack[len(stack) - 1].pre
else:
parent = 0
db.AddTuple(x.pre, pre, parent, x.type, x.data)
pre += 1
if event == "CHARACTERS" and node.data != "\n" and node.data != " ":
db.AddTuple(pre, pre + 1, pre - 1, "text", node.data)
pre += 2
x = stack.pop()
parent = -1
db.AddTuple(x.pre, pre, parent, x.type, x.data)
return db
开发者ID:roolin,项目名称:xmlbd,代码行数:26,代码来源:pullParse.py
示例12: __init__
def __init__(self, gmlFile):
self.gmlFile = gmlFile
_gml = open(gmlFile,'rb')
self.gml = _gml
self._filesize = float(os.fstat(_gml.fileno()).st_size)
self.events = pulldom.parse(_gml)
self.numFeatures = 0
开发者ID:opengeogroep,项目名称:AERIUS-QGIS-plugins,代码行数:7,代码来源:imaerread.py
示例13: _initFromFile
def _initFromFile(self, path):
kanjiDicFile = open(path)
events = pulldom.parse(kanjiDicFile)
for (event, node) in events:
if event == pulldom.START_ELEMENT:
if node.tagName.lower() == 'character':
events.expandNode(node)
self._processNode(node)
开发者ID:josephsavona,项目名称:anki-iknow-importer,代码行数:8,代码来源:kanjidic2_parser.py
示例14: __init__
def __init__(self, stream):
self._items = pulldom.parse(XMLStream(stream), bufsize = 256)
self._item = None # The current item
self._next = None # 1 item pushback buffer
self.kind = None
self.name = None
self.attrs = None
self.value = None
开发者ID:apanda,项目名称:splunk-sdk-python,代码行数:8,代码来源:results.py
示例15: parse
def parse(xmlfile, element_name):
# parses the attributes of all nodes with element_name and returns a list of namedtuples
# @note the first node in xmlfile will determine the set of attributes
# @note attribute names which are also python keywords will be prefixed with 'attr_'
elementType = [] # mutable, will be [namedtuple]
xml_doc = pulldom.parse(xmlfile)
return [get_attrs(parsenode, elementType, element_name) for event, parsenode in xml_doc
if event == pulldom.START_ELEMENT and parsenode.localName == element_name]
开发者ID:k0emt,项目名称:macts,代码行数:8,代码来源:__init__.py
示例16: process_map
def process_map(filename):
keys = {"lower": 0, "lower_colon": 0, "problemchars": 0, "other": 0}
tree=pulldom.parse(filename)
for event, node in tree:
if event==pulldom.START_ELEMENT:
keys = key_type(node, keys)
return keys
开发者ID:idati,项目名称:P2-DataWranleOpenStreet-shared-2015-04-16,代码行数:9,代码来源:tags.py
示例17: get_wk_nodes
def get_wk_nodes():
events = pulldom.parse(sys.stdin)
try:
for (event, node) in events:
if event == pulldom.START_ELEMENT and node.tagName == "page":
events.expandNode(node)
yield node
except Exception as e:
sys.stderr.write(str(e)+"\n")
开发者ID:Fabiensk,项目名称:osm-enrich,代码行数:9,代码来源:wk_offline.py
示例18: testXSLT
def testXSLT(self):
file = "../../xml_files_windows/optional/xslt.xsl"
tagName = "xsl:stylesheet"
doc = _PULLDOM.parse(file)
for event, node in doc:
if event == _PULLDOM.START_ELEMENT and node.tagName == tagName:
doc.expandNode(node)
self.assertEqual("xsl:stylesheet", node.nodeName)
开发者ID:RUB-NDS,项目名称:DTD-Attacks,代码行数:9,代码来源:testPulldom.py
示例19: testXInclude
def testXInclude(self):
file = "../../xml_files_windows/xinclude.xml"
tagName = "data"
doc = _PULLDOM.parse(file)
for event, node in doc:
if event == _PULLDOM.START_ELEMENT and node.tagName == tagName:
doc.expandNode(node)
self.assertEqual("xi:include", node.firstChild.nodeName)
开发者ID:RUB-NDS,项目名称:DTD-Attacks,代码行数:9,代码来源:testPulldom.py
示例20: WalkNodesForAttributes
def WalkNodesForAttributes(path):
"""Parse the xml file getting all attributes.
<venue>
<attribute>value</attribute>
</venue>
Returns:
type_name - The java-style name the top node will have. "Venue"
top_node_name - unadultured name of the xml stanza, probably the type of
java class we're creating. "venue"
attributes - {'attribute': 'value'}
"""
doc = pulldom.parse(path)
type_name = None
top_node_name = None
attributes = {}
level = 0
for event, node in doc:
# For skipping parts of a tree.
if level > 0:
if event == pulldom.END_ELEMENT:
level-=1
logging.warn('(%s) Skip end: %s' % (str(level), node))
continue
elif event == pulldom.START_ELEMENT:
logging.warn('(%s) Skipping: %s' % (str(level), node))
level+=1
continue
if event == pulldom.START_ELEMENT:
logging.warn('Parsing: ' + node.tagName)
# Get the type name to use.
if type_name is None:
type_name = ''.join([word.capitalize()
for word in node.tagName.split('_')])
top_node_name = node.tagName
logging.warn('Found Top Node Name: ' + top_node_name)
continue
typ = node.getAttribute('type')
child = node.getAttribute('child')
# We don't want to walk complex types.
if typ in COMPLEX:
logging.warn('Found Complex: ' + node.tagName)
level = 1
elif typ not in TYPES:
logging.warn('Found String: ' + typ)
typ = STRING
else:
logging.warn('Found Type: ' + typ)
logging.warn('Adding: ' + str((node, typ)))
attributes.setdefault(node.tagName, (typ, [child]))
logging.warn('Attr: ' + str((type_name, top_node_name, attributes)))
return type_name, top_node_name, attributes
开发者ID:316what,项目名称:foursquared.eclair,代码行数:56,代码来源:common.py
注:本文中的xml.dom.pulldom.parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论