我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用xml.dom.minidom.Element()。
def cleanXml(node): ## remove extraneous text; let the xml library do the formatting. hasElement = False nonElement = [] for ch in node.childNodes: if isinstance(ch, xml.Element): hasElement = True cleanXml(ch) else: nonElement.append(ch) if hasElement: for ch in nonElement: node.removeChild(ch) elif node.tagName == 'g': ## remove childless groups node.parentNode.removeChild(node)
def _element2dict(self, parent): """ ???????? dict """ d = {} for node in parent.childNodes: if not isinstance(node, minidom.Element): continue if not node.hasChildNodes(): continue if node.childNodes[0].nodeType == minidom.Node.ELEMENT_NODE: try: d[node.tagName] except KeyError: d[node.tagName] = [] d[node.tagName].append(self._element2dict(node)) elif len(node.childNodes) == 1 and node.childNodes[0].nodeType in [minidom.Node.CDATA_SECTION_NODE, minidom.Node.TEXT_NODE]: d[node.tagName] = node.childNodes[0].data return d
def render_GET(self, request): """ Render as HTML a listing of all known users with links to their personal resources. """ listing = Element('ul') for link, text in self._users(): linkElement = Element('a') linkElement.setAttribute('href', link + '/') textNode = Text() textNode.data = text linkElement.appendChild(textNode) item = Element('li') item.appendChild(linkElement) listing.appendChild(item) return self.template % {'users': listing.toxml()}
def __get_smf_dependencies(deps): """Given a minidom Element deps, search for the <service_fmri> elements inside it, and return the values as a list of strings.""" dependencies = [] for dependency in deps: fmris = dependency.getElementsByTagName("service_fmri") dep_type = dependency.getAttribute("type") grouping = dependency.getAttribute("grouping") delete = dependency.getAttribute("delete") # we don't include SMF path dependencies as these are often # not packaged files. if fmris and dep_type == "service" and \ grouping == "require_all" and \ delete != "true": for service_fmri in fmris: dependency = service_fmri.getAttribute("value") if dependency: dependencies.append(dependency) return dependencies
def unmarshal(element): rc = Bag() if isinstance(element, minidom.Element): for key in element.attributes.keys(): setattr(rc, key, element.attributes[key].value) childElements = [e for e in element.childNodes \ if isinstance(e, minidom.Element)] if childElements: for child in childElements: key = child.tagName if hasattr(rc, key): if type(getattr(rc, key)) <> type([]): setattr(rc, key, [getattr(rc, key)]) setattr(rc, key, getattr(rc, key) + [unmarshal(child)]) elif isinstance(child, minidom.Element) and \ (child.tagName == 'Details'): # make the first Details element a key setattr(rc,key,[unmarshal(child)]) #dbg: because otherwise 'hasattr' only tests #dbg: on the second occurence: if there's a #dbg: single return to a query, it's not a #dbg: list. This module should always #dbg: return a list of Details objects. else: setattr(rc, key, unmarshal(child)) else: #jec: we'll have the main part of the element stored in .text #jec: will break if tag <text> is also present text = "".join([e.data for e in element.childNodes \ if isinstance(e, minidom.Text)]) setattr(rc, 'text', text) return rc #unique items from a list from the cookbook
def unmarshal(element): rc = Bag() if isinstance(element, minidom.Element) and (element.tagName == 'Details'): rc.URL = element.attributes["url"].value childElements = [e for e in element.childNodes if isinstance(e, minidom.Element)] if childElements: for child in childElements: key = child.tagName if hasattr(rc, key): if type(getattr(rc, key)) <> type([]): setattr(rc, key, [getattr(rc, key)]) setattr(rc, key, getattr(rc, key) + [unmarshal(child)]) elif isinstance(child, minidom.Element) and (child.tagName == 'Details' or child.tagName == 'Item'): # make the first Details element a key setattr(rc,key,[unmarshal(child)]) #dbg: because otherwise 'hasattr' only tests #dbg: on the second occurence: if there's a #dbg: single return to a query, it's not a #dbg: list. This module should always #dbg: return a list of Details objects. else: setattr(rc, key, unmarshal(child)) else: rc = "".join([e.data for e in element.childNodes if isinstance(e, minidom.Text)]) if element.tagName == 'SalesRank': rc = rc.replace('.', '') rc = rc.replace(',', '') rc = int(rc) return rc
def createNode(root, nodeName, nodeText): """ Add an element node with nodeText to the 'root' element """ from xml.dom.minidom import Element, Text ele = Element(nodeName) text = Text() text.data = nodeText ele.appendChild(text) root.appendChild(ele)
def start_element_handler(self, name, attributes): if ' ' in name: uri, localname, prefix, qname = _parse_ns_name(self, name) else: uri = EMPTY_NAMESPACE qname = name localname = None prefix = EMPTY_PREFIX node = minidom.Element(qname, uri, prefix, localname) node.ownerDocument = self.document _append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr(_intern(self, 'xmlns:' + prefix), XMLNS_NAMESPACE, prefix, "xmlns") else: a = minidom.Attr("xmlns", XMLNS_NAMESPACE, "xmlns", EMPTY_PREFIX) d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = uri d = a.__dict__ d['value'] = d['nodeValue'] = uri d['ownerDocument'] = self.document _set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i+1] if ' ' in aname: uri, localname, prefix, qname = _parse_ns_name(self, aname) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr(aname, EMPTY_NAMESPACE, aname, EMPTY_PREFIX) _attrs[aname] = a _attrsNS[(EMPTY_NAMESPACE, aname)] = a d = a.childNodes[0].__dict__ d['data'] = d['nodeValue'] = value d = a.__dict__ d['ownerDocument'] = self.document d['value'] = d['nodeValue'] = value d['ownerElement'] = node
def start_element_handler(self, name, attributes): if ' ' in name: uri, localname, prefix, qname = _parse_ns_name(self, name) else: uri = EMPTY_NAMESPACE qname = name localname = None prefix = EMPTY_PREFIX node = minidom.Element(qname, uri, prefix, localname) node.ownerDocument = self.document _append_child(self.curNode, node) self.curNode = node if self._ns_ordered_prefixes: for prefix, uri in self._ns_ordered_prefixes: if prefix: a = minidom.Attr(_intern(self, 'xmlns:' + prefix), XMLNS_NAMESPACE, prefix, "xmlns") else: a = minidom.Attr("xmlns", XMLNS_NAMESPACE, "xmlns", EMPTY_PREFIX) a.value = uri a.ownerDocument = self.document _set_attribute_node(node, a) del self._ns_ordered_prefixes[:] if attributes: node._ensure_attributes() _attrs = node._attrs _attrsNS = node._attrsNS for i in range(0, len(attributes), 2): aname = attributes[i] value = attributes[i+1] if ' ' in aname: uri, localname, prefix, qname = _parse_ns_name(self, aname) a = minidom.Attr(qname, uri, localname, prefix) _attrs[qname] = a _attrsNS[(uri, localname)] = a else: a = minidom.Attr(aname, EMPTY_NAMESPACE, aname, EMPTY_PREFIX) _attrs[aname] = a _attrsNS[(EMPTY_NAMESPACE, aname)] = a a.ownerDocument = self.document a.value = value a.ownerElement = node