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
python类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 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 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 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 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 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 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 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 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 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