SelectSingleNode IXMLDOMNode

时间:2013-09-03 08:39:56

标签: xml delphi delphi-xe3 ixmldomnode

我有一个XML文件,并使用IXMLDOMNode中的SelectSingleNode读取它。 (DELPHI-XE3)

XMLResult:IXMLDOMNode;

XMLResult.xml位于

之下
<redlineaudit>
  <doclist>
    <doc id="0" order="0"/>
    <doc id="1" order="1"/>
  </doclist>
  <report>
    <redlineparagraph index="0" id="0" sourcedocid="0" cellrow="-1" cellcol="-1">
      <operation index="0" value="insert"/>
      <operation index="1" value="insert"/>
      <move index="0" value="0"/>
      <move index="1" value="0"/>
    </redlineparagraph>
    <redlinephraselist index="0" phrasehassomeworddifferences="false">
      <sourceparaid index="0" value="0"/>
      <sourceparaid index="1" value="1"/>
      <phrases>
        <phrase index="0">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="0" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="0" hasworddifference="false"/>
              <wordproperty index="1" id="1" hasworddifference="false"/>
              <wordproperty index="2" id="2" hasworddifference="false"/>
              <wordproperty index="3" id="3" hasworddifference="false"/>
              <wordproperty index="4" id="4" hasworddifference="false"/>
              <wordproperty index="5" id="5" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
        <phrase index="1">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="1" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="6" hasworddifference="false"/>
              <wordproperty index="1" id="7" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
        <phrase index="2">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="0" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="6" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
      </phrases>
      <references/>
    </redlinephraselist>
    <redlineparagraph index="1" id="1" sourcedocid="0" cellrow="-1" cellcol="-1">
      <operation index="0" value="insert"/>
      <operation index="1" value="insert"/>
      <move index="0" value="0"/>
      <move index="1" value="0"/>
    </redlineparagraph>
    <redlinephraselist index="0" phrasehassomeworddifferences="false">
      <sourceparaid index="0" value="0"/>
      <sourceparaid index="1" value="1"/>
      <phrases>
        <phrase index="0">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="0" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="0" hasworddifference="false"/>
              <wordproperty index="1" id="1" hasworddifference="false"/>
              <wordproperty index="2" id="2" hasworddifference="false"/>
              <wordproperty index="3" id="3" hasworddifference="false"/>
              <wordproperty index="4" id="4" hasworddifference="false"/>
              <wordproperty index="5" id="5" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
        <phrase index="1">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="1" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="6" hasworddifference="false"/>
              <wordproperty index="1" id="7" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
        <phrase index="2">
          <phraseversion index="0" deletedocid="-1" moved="false" sourcedocid="0" phrasehassomeworddifferences="false">
            <wordproperties>
              <wordproperty index="0" id="6" hasworddifference="false"/>
            </wordproperties>
          </phraseversion>
        </phrase>
      </phrases>
      <references/>
    </redlinephraselist>
  </report>
</redlineaudit>

我的问题是,如何阅读循环中的每一项?

redlineparagraph index = 0 操作指数= 0

操作指数= 1

操作值='插入'

redlinephraselist index = 0

sourceparaid index = 0

redlineparagraph index = 1

操作指数= 0

操作指数= 1

操作值='插入'

redlinephraselist index = 0

sourceparaid index = 0

.......

...谢谢

1 个答案:

答案 0 :(得分:2)

您的示例列表对我来说没有多大意义,但可能就是这样:

uses
  MSXML;

procedure ListValues(const FileName: string; Strings: TStrings);
var
  I, J: Integer;
  Document: IXMLDOMDocument;
  AttributeNode: IXMLDOMNode;
  ReportSubnodes: IXMLDOMNodeList;
  OperationNodes: IXMLDOMNodeList;
  ParameterNodes: IXMLDOMNodeList;
begin
  Document := CoDOMDocument.Create;
  if Assigned(Document) and Document.load(FileName) then
  begin
    // select all direct child nodes of the redlineaudit/report/ node
    ReportSubnodes := Document.selectNodes('//redlineaudit/report/node()');
    // check if the redlineaudit/report/ node was found and if so, then...
    if Assigned(ReportSubnodes) then
    begin
      // lock the output string list for update
      Strings.BeginUpdate;
      try
        // iterate all direct children of the redlineaudit/report/ node
        for I := 0 to ReportSubnodes.length - 1 do
        begin
          // try to find the "index" attribute of the iterated child node
          AttributeNode := ReportSubnodes[I].attributes.getNamedItem('index');
          // and if the "index" attribute is found, add a line to the list
          if Assigned(AttributeNode) then
            Strings.Add(Format('%s index = %s', [ReportSubnodes[I].nodeName, AttributeNode.nodeValue]));
          // select all "operation" child nodes of the iterated child node
          OperationNodes := ReportSubnodes[I].selectNodes('.//operation');
          // if some were found, then...
          if Assigned(OperationNodes) then
          begin
            // iterate those "operation" nodes
            for J := 0 to OperationNodes.length - 1 do
            begin
              // and again, try to find the the "index" attribute
              AttributeNode := OperationNodes[J].attributes.getNamedItem('index');
              // if there is an "index" attribute, then...
              if Assigned(AttributeNode) then
              begin
                // add an item to the output list
                Strings.Add(Format('%s index = %s', [OperationNodes[J].nodeName, AttributeNode.nodeValue]));
                // if the "index" attribute value is "1", then...
                if AttributeNode.nodeValue = '1' then
                begin
                  // find the "value" attribute
                  AttributeNode := OperationNodes[J].attributes.getNamedItem('value');
                  // if there is a "value" attribute, add an item to the output list
                  if Assigned(AttributeNode) then
                    Strings.Add(Format('%s value = ''%s''', [OperationNodes[J].nodeName, AttributeNode.nodeValue]));
                end;
              end;
            end;
          end;
          // select all "sourceparaid" child nodes of the iterated child node
          ParameterNodes := ReportSubnodes[I].selectNodes('.//sourceparaid');
          // if some were found, then...
          if Assigned(ParameterNodes) then
          begin
            // iterate those "sourceparaid" nodes
            for J := 0 to ParameterNodes.length - 1 do
            begin
              // and again, try to find the the "index" attribute
              AttributeNode := ParameterNodes[J].attributes.getNamedItem('index');
              // if there is an "index" attribute and this attribute has value "0",
              // add an item to the output list
              if Assigned(AttributeNode) and (AttributeNode.nodeValue = '0') then
                Strings.Add(Format('%s index = %s', [ParameterNodes[J].nodeName, AttributeNode.nodeValue]));
            end;
          end;
        end;
      finally
        // unlock the output string list for update
        Strings.EndUpdate;
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListValues('c:\File.xml', Memo1.Lines);
end;

我使用上面的代码从您的XML文件中获得了这个结果:

enter image description here