python 读xml xml path 写法。

本文介绍了如何在Python中使用xml.etree.ElementTree模块处理XML文件,特别是聚焦于XPath表达式的应用,包括选择节点、获取属性和处理命名空间等关键操作。通过实例解析XML数据,帮助开发者更高效地读取和操作XML文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<XMLTool>
<DataFilePath>C:/jack/d/workspace/Bam_Test/BAM Testing_Data.xml</DataFilePath>
<ScenariosFilePath>C:/jack/d/workspace/Bam_Test/BAM Testing_Scenarios.xml</ScenariosFilePath>
<SelectAllDataPreparationDataRows>
<Mode>SELECTALL</Mode>
<DataRowsXpath>//Table[contains(@name,'Data Preparation')]/Rows/Row/@isEnabled</DataRowsXpath>
</SelectAllDataPreparationDataRows>
<RemoveAllEmptyRows>
<DataRowsXpath>//Table[contains(@name,'Data Preparation')]/Rows/Row</DataRowsXpath>
</RemoveAllEmptyRows>
<AddTheTestURLForAllScenaios>
<Mode>ADDURL</Mode>
<ScenaiorsXpath>//Scenario/@url</ScenaiorsXpath>
<AddedURL>234</AddedURL>
</AddTheTestURLForAllScenaios>
<RemoveScenariosURL>
<ScenariosXpath>//Scenario/@url</ScenariosXpath>
</RemoveScenariosURL>
<SelectAllRegressionScenarios>
<Mode>SELECTALL</Mode>
<DataRowsXpath>//Scenarios/Scenario[contains(@name,'RT_')]/@isEnabled</DataRowsXpath>
</SelectAllRegressionScenarios>
<ChangeOrder>
<TargetScenarioName>RT_Import Entity -AccountCSVError</TargetScenarioName>
</ChangeOrder>
<SelectAllSteps>
<Mode>SELECTALL</Mode>
<ScenariosStepsXpath>//Scenarios/Scenario[contains(@name,'RT_')]/Steps/Step/@isEnabled</ScenariosStepsXpath>
</SelectAllSteps>
<UnSelectAllScenarios>
<ScenariosXpath>//Scenarios/Scenario/@isEnabled</ScenariosXpath>
</UnSelectAllScenarios>
<SelectYourFirm>
<DataRowsXpath>//Table[@name='Login Password']/Rows/Row</DataRowsXpath>
<Name>jack</Name>
</SelectYourFirm>

</XMLTool>


import xml.etree.ElementTree as ET
import os
def getXMLConfigNodeText(filePath,xPath):
    if (os.path.isfile(filePath)==False):
        return;
    root=ET.parse(filePath).getroot();
    nodetext=root.find(xPath).text
    if (nodetext==""):
        print("please check the node name is right!")
    #print(nodetext)
    return nodetext




import ReadXmlConfig
if __name__ == '__main__':
    scenarioFilePath=ReadXmlConfig.getXMLConfigNodeText("C:/HackXMLConfig.xml","ScenariosFilePath")
    xpath=ReadXmlConfig.getXMLConfigNodeText("C:/HackXMLConfig.xml","AddTheTestURLForAllScenaios/ScenaiorsXpath")
    url=ReadXmlConfig.getXMLConfigNodeText("C:/HackXMLConfig.xml", "AddTheTestURLForAllScenaios/AddedURL")
    print(scenarioFilePath)
    print(xpath)
    print(url)

默认 getroot =XMLTool 所以 第一可以写成 ScenariosFilePath ,默认root 可以省略。






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值