我是xml的新手,我正在使用带有Javascript的testcomplete。我已经嵌套了xml,我正在粘贴下面的一小部分。
我想在rowtype category =“ExitRow”职业空闲时提取rownumber和列代码营销座位类型为E.
<Row rowNumber="011">
<RowCharacteristics>
<RowType category="ExitRow"/>
</RowCharacteristics>
<Seats>
<Seat occupation="Free" columnCode="A">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
<Seat occupation="Free" columnCode="B">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="1A"/>
<PhysicalSeatType category="1B"/>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
<Seat occupation="Free" columnCode="C">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="1A"/>
<PhysicalSeatType category="1B"/>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
<Seat occupation="Free" columnCode="D">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="1A"/>
<PhysicalSeatType category="1B"/>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
<Seat occupation="Free" columnCode="E">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="1A"/>
<PhysicalSeatType category="1B"/>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
<Seat occupation="Free" columnCode="F">
<MarketingSeatType category="E"/>
<PhysicalSeatTypes>
<PhysicalSeatType category="1A"/>
<PhysicalSeatType category="1B"/>
<PhysicalSeatType category="E"/>
</PhysicalSeatTypes>
</Seat>
</Seats>
</Row>
我已经编写了一些代码来在测试完成时打开xml,但不确定这是否正确。
Doc = Sys.OleObject("Msxml2.DOMDocument.4.0");
Doc.async = false;
Doc.load("d:\\MyFile.xml");
Node = Doc.documentElement;
答案 0 :(得分:2)
最好的方法是使用XPath。您可以在this MSDN article中找到很多示例。
以下是适合您的代码:
function test()
{
var Doc = Sys.OleObject("Msxml2.DOMDocument.4.0");
Doc.async = false;
Doc.load("d:\\MyFile.xml");
var row = Doc.selectSingleNode('//Row[RowCharacteristics/RowType/@category="ExitRow"]');
var rowNumber = row.getAttribute("rowNumber");
Log.Message("Row number is " + rowNumber);
var cCodes = row.selectNodes('Seats/Seat[@occupation="Free" and MarketingSeatType/@category="E"]/@columnCode');
for (var i = 0; i < cCodes.length; i++)
Log.Message("Column code is " + cCodes.item(i).value);
}