【问题标题】:Making xml query in Python script在 Python 脚本中进行 xml 查询
【发布时间】:2014-05-25 23:27:16
【问题描述】:
module_rm = "resource-mgr"
def get_data_name(self, path):
    dataClass = "logicalRouterInterface"
    dataProp = "dn"
    msg = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <configResolveClass cookie="""+ self.authenticate()+ """ inHierarchical="false" classId=""" + "\"" + dataClass + "\"" + """>
    <inFilter>
            <eq class=""" + "\"" + dataClass + "\"" + """
            property=""" + "\"" + dataProp + "\"" + """
            value=""" + "\"" + path + "\"" + """
        />
    </inFilter>
    </configResolveClass>"""
    xml_response = self.send_xml(msg, module_rm)

    root = ET.fromstring( xml_response )
    dataint = root.findall("./outConfigs/" + dataClass)
    print "Number of matching datainterface ", len(dataint)

变量路径包含值“org-root/org-tcs/org-edgerouter-csr1”但dataClass即“logicalRouterInterface”,dataProp即“dn”的确切值是“org-root/org-tcs/org- edgerouter-csr1/router-intf-data2" 但我不知道确切的值我只知道值的一部分。 此查询不返回匹配的接口,因为它需要准确的值。请帮助修改它,以便它根据这个部分值进行搜索。

【问题讨论】:

    标签: python xml search scripting contains


    【解决方案1】:

    我注意到您的输入 XML (msg) 缺少 cookie= 的引用。修复后可能会起作用:

        <configResolveClass cookie=\""""+ self.authenticate()+ """\" inHierarchical="false" classId=""" + "\"" + dataClass + "\"" + """>
    

    【讨论】:

    • 代码正在运行。但是在值属性中,我传递了部分字符串“org-root/org-tcs/org-edgerouter-csr1”,但它的完整值是“org-root/org-tcs/org-edgerouter-csr1/router-intf-data2” .因此,它返回 0 匹配。如果我传递完整的值,那么它会返回正确的。但我不知道完整的价值。
    • 您所拥有的是特定于应用程序的,我不知道它有什么帮助。对不起。
    猜你喜欢
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多