从javascript中提取数据

Extracting data from javascript

本文关键字:数据 提取 javascript      更新时间:2023-09-26

我对xml很陌生,我使用的是带有Javascript的testcomplete。我已经嵌套了xml,我正在粘贴下面的一小部分。

当rowtype category="ExitRow"职业为免费时,我想提取行号和列代码。市场营销座位类型为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;

最好的方法是使用XPath。您可以在MSDN的这篇文章中找到很多例子。

以下是适用于您的代码:

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);
}