我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.dom.minidom()。
def testGetElementsByTagNameNS(self): d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'> <minidom:myelem/> </foo>""" dom = parseString(d) elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom", "myelem") self.confirm(len(elems) == 1 and elems[0].namespaceURI == "http://pyxml.sf.net/minidom" and elems[0].localName == "myelem" and elems[0].prefix == "minidom" and elems[0].tagName == "minidom:myelem" and elems[0].nodeName == "minidom:myelem") dom.unlink()
def get_childnode_attributes(node_name, parent_node, attribute_name): """ This function gets the node attributes. Parameters ---------- node_name : str The name of the node to be retrieved. parent_node : xml minidom Node The parent node. attribute_name : str The name of the attributes to retrieve. Returns ------- values : list of str The values of the attributes of the node. """ attributes = [] for node in parent_node.getElementsByTagName(node_name): attributes.append(str(node.attributes[attribute_name].value)) return attributes
def create_childnode(node_name, parent_node, value, doc): """ This function creates a node. Parameters ---------- node_name : str The name of the node to be created. parent_node : xml minidom Node The parent node. value : str The value of the node. doc : xml minidom Document The document to append the created node into. """ childnode = doc.createElement(node_name) value = doc.createTextNode(value) childnode.appendChild(value) parent_node.appendChild(childnode)
def get_score(ind): """ This function gets the scores of an invidual. Parameters ---------- ind : xml minidom Node The individual node. Returns ------- scores : list of floats The scores of the individual """ score_list = get_childnode_values("score", ind) score_list_f = [] for score in score_list: score_list_f.append(float(score)) return score_list_f
def get_inputparam(ind): """ This function gets the input parameters (genotype) of an invidual. Parameters ---------- ind : xml minidom Node The individual node. Returns ------- input parameters : list of floats The input parameters of the individual """ input_list = get_childnode_values("inputparam", ind) input_list_f = [] for inputx in input_list: input_list_f.append(float(inputx)) return input_list_f
def get_derivedparam(ind): """ This function gets the derived parameters of an invidual. Parameters ---------- ind : xml minidom Node The individual node. Returns ------- derived parameters : list of floats The derived parameters of the individual """ derived_list = get_childnode_values("derivedparam", ind) #derived_list_f = [] #for derived in derived_list: # derived_list_f.append(float(derived)) return derived_list
def get_id(ind): """ This function gets the unique id of an invidual. Parameters ---------- ind : xml minidom Node The individual node. Returns ------- id : str The id of the individual """ identity = get_childnode_value("identity", ind) return identity
def get_inds_frm_xml(xml_filepath): """ This function gets the individuals minidom Node from an XML file. Parameters ---------- xml_filepath : str The file path of the XML file. Returns ------- individuals : list of xml minidom Node All the individuals in the xml file. """ doc = xml.dom.minidom.parse(xml_filepath) ind_list = doc.getElementsByTagName("individual") return ind_list
def inds_2_score_2dlist(inds): """ This function converts xml minidom individual node into score 2dlist. Parameters ---------- inds : list of xml minidom Node All the individuals to be converted. Returns ------- score_2dlist : 2dlist of floats The performance objectives of a population of individuals. """ score_2dlist = [] for ind in inds: scorelist = get_score(ind) score_2dlist.append(scorelist) return score_2dlist
def __init__(self): # ??minidom????? XML ?? DOMTree = xml.dom.minidom.parse("beijing.xml") sw = DOMTree.documentElement lines = sw.getElementsByTagName("l") self.line_array = [] self.acc_name_map = { } for line in lines: station_array = [] stations = line.getElementsByTagName("p") for station in stations: station = Station(station) if station.lb != "": self.acc_name_map[station.acc] = station.lb station_array.append(station) self.line_array.append(Line(line, station_array))
def map_node_to_class(self, impl_node): try: return { xml.dom.Node.ELEMENT_NODE: nodes.Element, xml.dom.Node.ATTRIBUTE_NODE: nodes.Attribute, xml.dom.Node.TEXT_NODE: nodes.Text, xml.dom.Node.CDATA_SECTION_NODE: nodes.CDATA, # EntityReference not supported by minidom #xml.dom.Node.ENTITY_REFERENCE: nodes.EntityReference, xml.dom.Node.ENTITY_NODE: nodes.Entity, xml.dom.Node.PROCESSING_INSTRUCTION_NODE: nodes.ProcessingInstruction, xml.dom.Node.COMMENT_NODE: nodes.Comment, xml.dom.Node.DOCUMENT_NODE: nodes.Document, xml.dom.Node.DOCUMENT_TYPE_NODE: nodes.DocumentType, xml.dom.Node.DOCUMENT_FRAGMENT_NODE: nodes.DocumentFragment, xml.dom.Node.NOTATION_NODE: nodes.Notation, }[impl_node.nodeType] except KeyError: raise exceptions.Xml4hImplementationBug( 'Unrecognized type for implementation node: %s' % impl_node)
def _writeXML(xmlnode): if isinstance(xmlnode, xml.dom.minidom.DocumentFragment): d = xml.dom.minidom.Document() d.childNodes += xmlnode.childNodes xmlnode = d s = xmlnode.toxml('utf-8') # for clean round-tripping, remove headers -- I have great and # specific worries that this will blow up later, but this margin # is too narrow to contain them if s.startswith(b('<?xml version="1.0" encoding="utf-8"?>')): s = s[38:] if s.startswith(b('<rdflibtoplevelelement>')): s = s[23:-24] if s == b('<rdflibtoplevelelement/>'): s = b('') return s # Cannot import Namespace/XSD because of circular dependencies
def elementtodict(self, parent): child = parent.firstChild if (not child): return None elif (child.nodeType == xml.dom.minidom.Node.TEXT_NODE): return child.nodeValue d={} while child is not None: if (child.nodeType == xml.dom.minidom.Node.ELEMENT_NODE): try: d[child.tagName] except KeyError: d[child.tagName]=[] d[child.tagName].append(self.elementtodict(child)) child = child.nextSibling return d
def _xml_to_json(element, stack, dictionary): ''' This method creates a json representation of the given xml structure. It traverses the dom tree and adds elements to the dictionary as it goes. ''' stack.append(element.nodeName) LOGGER.debug('Processing %s element.', element.nodeName) if ( element.firstChild and element.firstChild.nodeValue and len(element.firstChild.nodeValue.strip()) ): put_in_dictionary(dictionary, stack, parse_value(element.firstChild.nodeValue.strip())) else: # This line might be removed. put_in_dictionary(dictionary, stack, {}) for child in element.childNodes: if child.nodeType == dom.Node.ELEMENT_NODE: _xml_to_json(child, stack, dictionary) stack.pop()
def createDocument(self, nsuri, qname, doctype=None): """Create a new writable DOM document object.""" impl = xml.dom.minidom.getDOMImplementation() return impl.createDocument(nsuri, qname, doctype)
def loadDocument(self, data): """Load an xml file from a file-like object and return a DOM document instance.""" return xml.dom.minidom.parse(data)
def values(self): return self.list # This is a runtime guerilla patch for pulldom (used by minidom) so # that xml namespace declaration attributes are not lost in parsing. # We need them to do correct QName linking for XML Schema and WSDL. # The patch has been submitted to SF for the next Python version.
def getText(self, nodelist): """Get the text value of an XML tag (from the minidom doc).""" rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc)
def create_nonempty_doctype(): doctype = getDOMImplementation().createDocumentType("doc", None, None) doctype.entities._seq = [] doctype.notations._seq = [] notation = xml.dom.minidom.Notation("my-notation", None, "http://xml.python.org/notations/my") doctype.notations._seq.append(notation) entity = xml.dom.minidom.Entity("my-entity", None, "http://xml.python.org/entities/my", "my-notation") entity.version = "1.0" entity.encoding = "utf-8" entity.actualEncoding = "us-ascii" doctype.entities._seq.append(entity) return doctype
def testRenameOther(self): # We have to create a comment node explicitly since not all DOM # builders used with minidom add comments to the DOM. doc = xml.dom.minidom.getDOMImplementation().createDocument( xml.dom.EMPTY_NAMESPACE, "e", None) node = doc.createComment("comment") self.assertRaises(xml.dom.NotSupportedErr, doc.renameNode, node, xml.dom.EMPTY_NAMESPACE, "foo") doc.unlink()
def get_childnode_values(node_name, parent_node): """ This function gets the node value. Parameters ---------- node_name : str The name of the node to be retrieved. parent_node : xml minidom Node The parent node. Returns ------- values : list of str The values of the node. """ values = [] for node in parent_node.getElementsByTagName(node_name): node_value = "" for cnode in node.childNodes: if cnode.nodeType == Node.TEXT_NODE: #in case the text node is separated tvalue = str(cnode.nodeValue) node_value = node_value + tvalue values.append(node_value) return values
def get_childnode_value(node_name, parent_node): """ This function gets the node value. Parameters ---------- node_name : str The name of the node to be retrieved. parent_node : xml minidom Node The parent node. Returns ------- value : str The value of the node. """ nodes_list = parent_node.getElementsByTagName(node_name) num_nodes = len(nodes_list) if num_nodes > 1: raise Exception("more than one node!!") elif num_nodes == 0: raise Exception("no nodes!!") else: values = [] for node in nodes_list: node_value = "" for cnode in node.childNodes: if cnode.nodeType == Node.TEXT_NODE: #in case the text node is separated tvalue = str(cnode.nodeValue) node_value = node_value + tvalue values.append(node_value) return values[0]
def edit_nodevalue(node_name, parent_node, change_value): """ This function gets the node value. Parameters ---------- node_name : str The name of the node to be retrieved. parent_node : xml minidom Node The parent node. change_value : str The new value of the node. """ nodes_list = parent_node.getElementsByTagName(node_name) num_nodes = len(nodes_list) if num_nodes > 1: raise Exception("more than one node!!") elif num_nodes == 0: raise Exception("no nodes!!") else: for node in nodes_list: for cnode in node.childNodes: if cnode.nodeType == Node.TEXT_NODE: cnode.nodeValue = change_value
def extract_pareto_front_inds(inds, min_max_list): """ This function extract the Pareto front from the list of score_2dlist. Parameters ---------- inds : list of xml minidom Node The individuals to extract the Pareto front from. min_max_list : list of ints The min max list is in this format, [0,1]. 0 = minimise, 1 = maximise. The min max list must correspond to the two result lists. Returns ------- pareto front : list of xml minidom Node The population of individuals on the front. non pareto front : list of xml minidom Node The population of individuals not on the front. """ pareto_front = [] non_pareto_front = [] score_2dlist = inds_2_score_2dlist(inds) for ind in inds: score_list = get_score(ind) if (len(score_list)-1) !=0: if on_pareto_front(score_list, score_2dlist, min_max_list): pareto_front.append(ind) else: non_pareto_front.append(ind) return pareto_front, non_pareto_front
def extract_save_yang(dirname,capa): modre = re.compile("module=([^&\?]+)").search(capa) if modre: global count_yangs, count_yangs_written count_yangs += 1 modname = modre.groups()[0] c.send_msg(get_schema_msg(modname)) reply = c.recv_msg() d = xml.dom.minidom.parseString(reply) if d is not None: d = d.firstChild if d is not None: d = d.firstChild strip(d) if (d.namespaceURI == nc_ns and d.localName == 'rpc-error'): print "Could not get schema for %s.yang"%modname return ## Can't find some good way to extract actual YANG module ## from minidom, so here goes: yangre = re.compile(".*(module +[a-zA-Z0-9_-]+ *{.+}).*</data>.*", re.MULTILINE|re.DOTALL).search(reply) if yangre: yangtext = yangre.groups()[0] yangtext = yangtext.replace("<","<").\ replace(">",">").\ replace("&","&").\ replace(""","\'") filename = "%s/%s.yang"%(dirname,modname) try: f = open(filename,"w") print>>f,yangtext f.close() print "Wrote schema into %s"%filename count_yangs_written += 1 except: print "Could not write schema into %s"%filename else: print "Could not parse schema for %s.yang"%modname