解析XML
发布时间:2020-12-16 23:45:07 所属栏目:百科 来源:网络整理
导读:通过xml.dom.minidom解析 XML中的内容: ?xml version="1.0" encoding="UTF-8" ?configuration property namemodbus_ip/name value127.0.0.1/value describename的描述内容/describe /property/configuration Python3 脚本内容: import xml.dom.minidom as
通过xml.dom.minidom解析
XML中的内容: <?xml version="1.0" encoding="UTF-8" ?> <configuration> <property> <name>modbus_ip</name> <value>127.0.0.1</value> <describe>name的描述内容</describe> </property> </configuration> Python3 脚本内容: import xml.dom.minidom as xminidom from os.path import dirname,abspath # 解析XML文件,获取相应数据 def introduce_args(input_name): """ :param input_name: string :return: string """ setting_path = dirname(dirname(abspath(__file__))) + ‘/conf/setting.xml‘ dom_tree = xminidom.parse(setting_path) collection = dom_tree.documentElement properties = collection.getElementsByTagName("property") # 找到标签是property的 for message in properties: name = message.getElementsByTagName(‘name‘)[0] # 在标签是property的里面找标签是name的 if name.childNodes[0].data == input_name: value = message.getElementsByTagName(‘value‘)[0] return value.childNodes[0].data if __name__ == ‘__main__‘: v = introduce_args("modbus_ip") # 输入想要查询的name,获取value print(v) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |