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()
python类minidom()的实例源码
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 _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 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 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.
internalapi.py 文件源码
项目:CommunityCellularManager
作者: facebookincubator
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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 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.