我是新手,有1周的编写Python脚本的经验。
我正在尝试编写一个通用解析器(我以后所有工作的图书馆),以解析任何输入XML而无需任何标签知识。
解析输入XML。从XML获取值,然后根据标签设置值。在其余工作中使用这些值。我正在使用“ xml.etree.ElementTree”库,并且能够以下面提到的方式解析XML。
#!/usr/bin/python import os import xml.etree.ElementTree as etree import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logger.info('start reading XML property file') filename = "mood_ib_history_parameters_DEV.xml" logger.info('getting the current location') __currentlocation__ = os.getcwd() __fullpath__ = os.path.join(__currentlocation__,filename) logger.info('start parsing the XML property file') tree = etree.parse(__fullpath__) root = tree.getroot() hive_db = root.find("hive_db").text EDGE_HIVE_CONN = root.find("EDGE_HIVE_CONN").text target_dir = root.find("target_dir").text to_email_alias = root.find("to_email_alias").text to_email_cc = root.find("to_email_cc").text from_email_alias = root.find("from_email_alias").text dburl = root.find("dburl").text SQOOP_EDGE_CONN = root.find("SQOOP_EDGE_CONN").text user_name = root.find("user_name").text password = root.find("password").text IB_log_table = root.find("IB_log_table").text SR_DG_master_table = root.find("SR_DG_master_table").text SR_DG_table = root.find("SR_DG_table").text logger.info('Hive DB %s', hive_db) logger.info('Hive DB %s', hive_db) logger.info('Edge Hive Connection %s', EDGE_HIVE_CONN) logger.info('Target Directory %s', target_dir) logger.info('To Email address %s', to_email_alias) logger.info('CC Email address %s', to_email_cc) logger.info('From Email address %s', from_email_alias) logger.info('DB URL %s',dburl) logger.info('Sqoop Edge node connection %s',SQOOP_EDGE_CONN) logger.info('Log table name %s',IB_log_table) logger.info('Master table name %s',SR_DG_master_table) logger.info('Data governance table name %s',SR_DG_table)
现在的问题是,如果我想在不了解标签和元素的情况下解析XML并使用值怎么做。我已经看过多个教程,但是所有这些都通过使用如下标记来帮助我解析XML。
SQOOP_EDGE_CONN = root.find(“SQOOP_EDGE_CONN”).text任何人都可以指向正确的教程或库或代码片段来动态解析XML。