nas-tools/app/utils/dom_utils.py
2023-02-13 12:52:00 +08:00

31 lines
877 B
Python

class DomUtils:
@staticmethod
def tag_value(tag_item, tag_name, attname="", default=None):
"""
解析XML标签值
"""
tagNames = tag_item.getElementsByTagName(tag_name)
if tagNames:
if attname:
attvalue = tagNames[0].getAttribute(attname)
if attvalue:
return attvalue
else:
firstChild = tagNames[0].firstChild
if firstChild:
return firstChild.data
return default
@staticmethod
def add_node(doc, parent, name, value=None):
"""
添加一个DOM节点
"""
node = doc.createElement(name)
parent.appendChild(node)
if value is not None:
text = doc.createTextNode(str(value))
node.appendChild(text)
return node