我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用xml.dom.minidom.Text()。
def character_data_handler_cdata(self, data): childNodes = self.curNode.childNodes if self._cdata: if ( self._cdata_continue and childNodes[-1].nodeType == CDATA_SECTION_NODE): childNodes[-1].appendData(data) return node = self.document.createCDATASection(data) self._cdata_continue = True elif childNodes and childNodes[-1].nodeType == TEXT_NODE: node = childNodes[-1] value = node.data + data d = node.__dict__ d['data'] = d['nodeValue'] = value return else: node = minidom.Text() d = node.__dict__ d['data'] = d['nodeValue'] = data d['ownerDocument'] = self.document _append_child(self.curNode, node)
def character_data_handler_cdata(self, data): childNodes = self.curNode.childNodes if self._cdata: if ( self._cdata_continue and childNodes[-1].nodeType == CDATA_SECTION_NODE): childNodes[-1].appendData(data) return node = self.document.createCDATASection(data) self._cdata_continue = True elif childNodes and childNodes[-1].nodeType == TEXT_NODE: node = childNodes[-1] value = node.data + data node.data = value return else: node = minidom.Text() node.data = data node.ownerDocument = self.document _append_child(self.curNode, node)
def recordToSource(recordData): """ Compose Xml source code from a record's data which is a dict object. """ from xml.dom.minidom import Document, Text import re doc = Document() root = doc.createElement("log") doc.appendChild(root) items = dict(recordData).items() fields = Record.convertFields(items, False) # sort the fields data according to the definition order orders = {k: v['order'] for k, v in Record.fields.items()} sortKey = lambda x: orders[x[0]] fields = sorted(fields.items(), key=sortKey) for name, value in fields: XmlStorage.createNode(root, name, value) xmlCode = doc.toprettyxml() xmlCode = re.sub('\t', ' ' * 4, xmlCode) # replace tabs with spaces return xmlCode
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 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 character_data_handler(self, data): childNodes = self.curNode.childNodes if childNodes and childNodes[-1].nodeType == TEXT_NODE: node = childNodes[-1] d = node.__dict__ d['data'] = d['nodeValue'] = node.data + data return node = minidom.Text() d = node.__dict__ d['data'] = d['nodeValue'] = node.data + data d['ownerDocument'] = self.document _append_child(self.curNode, node)
def build_text(self, data): """Builds XML text element :param data: :return: :class:`~xml.dom.minidom.Text` """ t = minidom.Text() t.data = data return t
def character_data_handler(self, data): childNodes = self.curNode.childNodes if childNodes and childNodes[-1].nodeType == TEXT_NODE: node = childNodes[-1] node.data = node.data + data return node = minidom.Text() node.data = node.data + data node.ownerDocument = self.document _append_child(self.curNode, node)
def xml_set_cdata(_node, _value, _lowercase=False): """Helper to set character data in an XML tree""" if _value is not None and _value != "": sec = Text() if _value is str: _value = quote(_value) if _lowercase: # Force lowercase. sec.data = _value.lower() else: sec.data = _value _node.appendChild(sec)
def xml_find_non_text_child(_node): """Finds the first child that is not of the Text node type.""" nodelist = _node.childNodes for node in nodelist: if node.nodeType != node.TEXT_NODE: return node return None
def NodeSetAttr(node, name, value): attr = minidom.Attr(name) text = minidom.Text() text.data = value attr.childNodes[0] = text node._attrs[name] = attr
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 set_title(depot, doc, feed, update_ts): """This function attaches the necessary RSS/Atom feed elements needed to provide title, author and contact information to the provided xmini document object using the provided feed object and update time. """ t = doc.createElement("title") ti = xmini.Text() ti.replaceWholeText(depot.cfg.get_property("pkg_bui", "feed_name")) t.appendChild(ti) feed.appendChild(t) l = doc.createElement("link") l.setAttribute("href", cherrypy.url()) l.setAttribute("rel", "self") feed.appendChild(l) # Atom requires each feed to have a permanent, universally unique # identifier. i = doc.createElement("id") it = xmini.Text() netloc, path = urlparse(cherrypy.url())[1:3] netloc = netloc.split(":", 1)[0] tag = "tag:{0},{1}:{2}".format(netloc, update_ts.strftime("%Y-%m-%d"), path) it.replaceWholeText(tag) i.appendChild(it) feed.appendChild(i) # Indicate when the feed was last updated. u = doc.createElement("updated") ut = xmini.Text() ut.replaceWholeText(dt_to_rfc3339_str(update_ts)) u.appendChild(ut) feed.appendChild(u) # Add our icon. i = doc.createElement("icon") it = xmini.Text() it.replaceWholeText(depot.cfg.get_property("pkg_bui", "feed_icon")) i.appendChild(it) feed.appendChild(i) # Add our logo. l = doc.createElement("logo") lt = xmini.Text() lt.replaceWholeText(depot.cfg.get_property("pkg_bui", "feed_logo")) l.appendChild(lt) feed.appendChild(l)