Python xml.dom.minidom 模块,Node() 实例源码
我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用xml.dom.minidom.Node()。
def save_list(self, doc, items, prop_node):
items_node = doc.createElement('items')
prop_node.appendChild(items_node)
for item in items:
item_node = doc.createElement('item')
items_node.appendChild(item_node)
if isinstance(item, Node):
item_node.appendChild(item)
else:
text_node = doc.createTextNode(item)
item_node.appendChild(text_node)
def marshal_object(self, obj, doc=None):
if not doc:
doc = self.new_doc()
if not doc:
doc = self.doc
obj_node = doc.createElement('object')
if obj.id:
obj_node.setAttribute('id', obj.id)
obj_node.setAttribute('class', '%s.%s' % (obj.__class__.__module__,
obj.__class__.__name__))
root = doc.documentElement
root.appendChild(obj_node)
for property in obj.properties(hidden=False):
prop_node = doc.createElement('property')
prop_node.setAttribute('name', property.name)
prop_node.setAttribute('type', property.type_name)
value = property.get_value_for_datastore(obj)
if value is not None:
value = self.encode_value(property, value)
if isinstance(value, list):
self.save_list(doc, value, prop_node)
elif isinstance(value, Node):
prop_node.appendChild(value)
else:
text_node = doc.createTextNode(six.text_type(value).encode("ascii", "ignore"))
prop_node.appendChild(text_node)
obj_node.appendChild(prop_node)
return doc
def get_meta_from_xml(xml_str, meta_name):
xml = clean_and_parse_xml(xml_str)
children = xml.childNodes
# children ideally contains a single element
# that is the parent of all survey elements
if children.length == 0:
raise ValueError(_("XML string must have a survey element."))
survey_node = children[0]
meta_tags = [n for n in survey_node.childNodes if
n.nodeType == Node.ELEMENT_NODE and
(n.tagName.lower() == "meta" or
n.tagName.lower() == "orx:meta")]
if len(meta_tags) == 0:
return None
# get the requested tag
meta_tag = meta_tags[0]
uuid_tags = [n for n in meta_tag.childNodes if
n.nodeType == Node.ELEMENT_NODE and
(n.tagName.lower() == meta_name.lower() or
n.tagName.lower() == u'orx:%s' % meta_name.lower())]
if len(uuid_tags) == 0:
return None
uuid_tag = uuid_tags[0]
return uuid_tag.firstChild.nodeValue.strip() if uuid_tag.firstChild\
else None
def save_list(self, doc, items, prop_node):
items_node = doc.createElement('items')
prop_node.appendChild(items_node)
for item in items:
item_node = doc.createElement('item')
items_node.appendChild(item_node)
if isinstance(item, Node):
item_node.appendChild(item)
else:
text_node = doc.createTextNode(item)
item_node.appendChild(text_node)
def marshal_object(self, obj, doc=None):
if not doc:
doc = self.new_doc()
if not doc:
doc = self.doc
obj_node = doc.createElement('object')
if obj.id:
obj_node.setAttribute('id', obj.id)
obj_node.setAttribute('class', '%s.%s' % (obj.__class__.__module__,
obj.__class__.__name__))
root = doc.documentElement
root.appendChild(obj_node)
for property in obj.properties(hidden=False):
prop_node = doc.createElement('property')
prop_node.setAttribute('name', property.name)
prop_node.setAttribute('type', property.type_name)
value = property.get_value_for_datastore(obj)
if value is not None:
value = self.encode_value(property, value)
if isinstance(value, list):
self.save_list(doc, value, prop_node)
elif isinstance(value, Node):
prop_node.appendChild(value)
else:
text_node = doc.createTextNode(six.text_type(value).encode("ascii", "ignore"))
prop_node.appendChild(text_node)
obj_node.appendChild(prop_node)
return doc
def CreateNode(name):
node = minidom.Node()
node.nodeName = name
node._attrs = {}
node.childNodes = []
return node
def save_list(self, doc, items, prop_node):
items_node = doc.createElement('items')
prop_node.appendChild(items_node)
for item in items:
item_node = doc.createElement('item')
items_node.appendChild(item_node)
if isinstance(item, Node):
item_node.appendChild(item)
else:
text_node = doc.createTextNode(item)
item_node.appendChild(text_node)
def marshal_object(self, obj, doc=None):
if not doc:
doc = self.new_doc()
if not doc:
doc = self.doc
obj_node = doc.createElement('object')
if obj.id:
obj_node.setAttribute('id', obj.id)
obj_node.setAttribute('class', '%s.%s' % (obj.__class__.__module__,
obj.__class__.__name__))
root = doc.documentElement
root.appendChild(obj_node)
for property in obj.properties(hidden=False):
prop_node = doc.createElement('property')
prop_node.setAttribute('name', property.name)
prop_node.setAttribute('type', property.type_name)
value = property.get_value_for_datastore(obj)
if value is not None:
value = self.encode_value(property, value)
if isinstance(value, list):
self.save_list(doc, value, prop_node)
elif isinstance(value, Node):
prop_node.appendChild(value)
else:
text_node = doc.createTextNode(six.text_type(value).encode("ascii", "ignore"))
prop_node.appendChild(text_node)
obj_node.appendChild(prop_node)
return doc
def get_meta_from_xml(xml_str, meta_name):
xml = clean_and_parse_xml(xml_str)
children = xml.childNodes
# children ideally contains a single element
# that is the parent of all survey elements
if children.length == 0:
raise ValueError(_("XML string must have a survey element."))
survey_node = children[0]
meta_tags = [n for n in survey_node.childNodes if
n.nodeType == Node.ELEMENT_NODE and
(n.tagName.lower() == "meta" or
n.tagName.lower() == "orx:meta")]
if len(meta_tags) == 0:
return None
# get the requested tag
meta_tag = meta_tags[0]
uuid_tags = [n for n in meta_tag.childNodes if
n.nodeType == Node.ELEMENT_NODE and
(n.tagName.lower() == meta_name.lower() or
n.tagName.lower() == u'orx:%s' % meta_name.lower())]
if len(uuid_tags) == 0:
return None
uuid_tag = uuid_tags[0]
return uuid_tag.firstChild.nodeValue.strip() if uuid_tag.firstChild\
else None
def _xml_node_to_dict(node, repeats=[]):
assert isinstance(node, minidom.Node)
if len(node.childNodes) == 0:
# there's no data for this leaf node
return None
elif len(node.childNodes) == 1 and \
node.childNodes[0].nodeType == node.TEXT_NODE:
# there is data for this leaf node
return {node.nodeName: node.childNodes[0].nodeValue}
else:
# this is an internal node
value = {}
for child in node.childNodes:
# handle CDATA text section
if child.nodeType == child.CDATA_SECTION_NODE:
return {child.parentNode.nodeName: child.nodeValue}
d = _xml_node_to_dict(child, repeats)
if d is None:
continue
child_name = child.nodeName
child_xpath = xpath_from_xml_node(child)
assert d.keys() == [child_name]
node_type = dict
# check if name is in list of repeats and make it a list if so
if child_xpath in repeats:
node_type = list
if node_type == dict:
if child_name not in value:
value[child_name] = d[child_name]
else:
raise InstanceMultipleNodeError(
_(u"Multiple nodes with the same name '%s'"
u" while not a repeat" % child_name))
else:
if child_name not in value:
value[child_name] = [d[child_name]]
else:
value[child_name].append(d[child_name])
if value == {}:
return None
else:
return {node.nodeName: value}
def _xml_node_to_dict(node, repeats=[]):
assert isinstance(node, minidom.Node)
if len(node.childNodes) == 0:
# there's no data for this leaf node
return None
elif len(node.childNodes) == 1 and \
node.childNodes[0].nodeType == node.TEXT_NODE:
# there is data for this leaf node
return {node.nodeName: node.childNodes[0].nodeValue}
else:
# this is an internal node
value = {}
for child in node.childNodes:
# handle CDATA text section
if child.nodeType == child.CDATA_SECTION_NODE:
return {child.parentNode.nodeName: child.nodeValue}
d = _xml_node_to_dict(child, repeats)
if d is None:
continue
child_name = child.nodeName
child_xpath = xpath_from_xml_node(child)
assert d.keys() == [child_name]
node_type = dict
# check if name is in list of repeats and make it a list if so
if child_xpath in repeats:
node_type = list
if node_type == dict:
if child_name not in value:
value[child_name] = d[child_name]
else:
raise InstanceMultipleNodeError(
_(u"Multiple nodes with the same name '%s'"
u" while not a repeat" % child_name))
else:
if child_name not in value:
value[child_name] = [d[child_name]]
else:
value[child_name].append(d[child_name])
if value == {}:
return None
else:
return {node.nodeName: value}