提问人:Jandak 提问时间:6/6/2023 最后编辑:Jandak 更新时间:6/6/2023 访问量:73
使用 TestComplete Javascript 从 XML 获取属性值
Get attribute value from XML with TestComplete Javascript
问:
我需要从元素 <testItem name=“Phase0” ...>
有人可以告诉我怎么做吗?我不知道该怎么做..
我尝试了一些东西来帮助xpath,但没有结果。
文档已加载,但我不知道如何获取属性值
function getAttrValue()
{
var fileName = "C:\\test.xml";
var doc = Sys.OleObject("Msxml2.DOMDocument.6.0");
doc.load(fileName);
var value = doc.selectSingleNode("/Root/testItems/children/testItem(1)/children/testItem(0)").enabled;
Log.Message(value);
}
XML 代码不完整...太长了
谢谢
<Root signature="{362A8EE5-675E-43A9-869D-06ECE80B0CB4}" version="14.0">
<testItems key="{18FBEBF5-E1A7-4B44-A78F-4BDB66ECB279}" version="14">
<children>
<testItem name="SETENVIRONMENT" key="{69AEF7E8-30A3-4AA3-B44A-F6F5A0812578}" group="True" parallel="False" enabled="True" testMoniker="" count="1" timeout="0" stopOnError="3" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<children>
<testItem name="Automate" key="{A9BBB245-02DE-4F1D-BEE8-9B3A576E0F04}" group="False" parallel="False" enabled="True" testMoniker="{088295FE-07C1-4A44-83FE-27198978BB3A}AutomateProcessRun" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
</children>
</testItem>
<testItem name="TESTCASES" key="{F27CAF00-67B1-44D6-9D6B-B8A1498AB787}" group="True" parallel="False" enabled="True" testMoniker="" count="1" timeout="0" stopOnError="3" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<children>
<testItem name="Phase0" key="{6D5E60D6-FC79-4ABE-9E4E-95625C081D45}" group="False" parallel="False" enabled="True" testMoniker="KDT\{B024E0F5-9C6E-43B4-BA21-D79A54AF1B61}" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
<testItem name="Phase1" key="{2D49F60C-20B6-4D73-8FC5-FA57B1AA6677}" group="False" parallel="False" enabled="True" testMoniker="KDT\{28D6EBB9-B449-407B-942B-E43C7A6E5E7B}" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
<testItem name="Phase2" key="{EB2033B4-6AB6-409C-8674-90B1CB4C2288}" group="False" parallel="False" enabled="True" testMoniker="KDT\{AB04AC00-EC2F-4CDE-AB21-95327EDC97AE}" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
<testItem name="Phase3" key="{8C5DC219-A594-4790-AF51-CA3FAD21F722}" group="False" parallel="False" enabled="True" testMoniker="KDT\{12B9102E-1E90-4D4F-8B1D-A1AD951BDC85}" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
<testItem name="Phase4" key="{8785B649-B1B5-4A7A-8543-21AD95718EC6}" group="False" parallel="False" enabled="True" testMoniker="KDT\{890BAA92-FF2C-468B-AF0C-80E397C63EE4}" count="1" timeout="0" stopOnError="1" stopOnException="2" description="" tmsData="" testCase="True">
<testParameters />
<environments activeCloud="CBT" />
<children />
</testItem>
</children>
</testItem>
</children>
</testItems>
</Root>
答:
0赞
Jack Fleeting
6/6/2023
#1
需要注意的是,我无法自己尝试,您的问题似乎与处理命名空间有关。一种简单的方法(并不总是有效)是完全避开它们。看看如果将
doc.selectSingleNode("/Root/testItems/children/testItem(1)/children/testItem(0)")
跟
doc.selectSingleNode("//*[local-name()="testItem"][@name="Phase0"]/@enabled")
评论
0赞
Jack Fleeting
6/6/2023
@Jandak 很高兴它对你有用!
评论