XML parsing, TXMLDocument

前端 未结 3 1795
南笙
南笙 2020-12-04 23:13

I have a problem with parsing XML.

How to get field values se_url and phrase?
I need to get link1_1, link1_2

3条回答
  •  醉酒成梦
    2020-12-04 23:15

    If you first include these 3 general purpose library routines ....

    uses XMLDoc, XMLIntf, xmldom;
    
    function CreateXMLDocument( var Owner1: TComponent): TXMLDocument;
    begin
    Owner1 := TComponent.Create( nil);
    result  := TXMLDocument.Create( Owner1);
    result.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull,
                       doAutoPrefix, doNamespaceDecl];
    result.DOMVendor := GetDOMVendor( 'MSXML');
    end;
    
    function XPATHSelect( const FocusNode: IXMLNode; const sXPath: string): TArray;
    var
      DomNodeSelect: IDomNodeSelect;
      DOMNode      : IDomNode;
      DocAccess    : IXmlDocumentAccess;
      Doc          : TXmlDocument;
      DOMNodes     : IDOMNodeList;
      iDOMNode     : integer;
    begin
    SetLength( result, 0);
    if assigned( FocusNode) and
       Supports( FocusNode.DOMNode, IDomNodeSelect, DomNodeSelect) then
        DOMNodes := DomNodeSelect.SelectNodes( sXPath);
    if not assigned( DOMNodes) then exit;
    SetLength( result, DOMNodes.Length);
    for iDOMNode := 0 to DOMNodes.Length - 1 do
      begin
      Doc := nil;
      DOMNode := DOMNodes.item[iDOMNode];
      if Supports( DOMNode, IXmlDocumentAccess, DocAccess) then
        Doc := DocAccess.DocumentObject;
      result[ iDOMNode] := TXmlNode.Create( DOMNode, nil, Doc) as IXMLNode;
      end
    end;
    
    
    function XPATHSelectFirst( const FocusNode: IXMLNode; const sXPath: string; var SelectedNode: IXMLNode): boolean;
    var
      DomNodeSelect: IDomNodeSelect;
      DOMNode      : IDomNode;
      DocAccess    : IXmlDocumentAccess;
      Doc          : TXmlDocument;
    begin
    SelectedNode := nil;
    if assigned( FocusNode) and
       Supports( FocusNode.DOMNode, IDomNodeSelect, DomNodeSelect) then
      DOMNode := DomNodeSelect.selectNode( sXPath);
    if assigned( DOMNode) and
       Supports( DOMNode.OwnerDocument, IXmlDocumentAccess, DocAccess) then
      Doc := DocAccess.DocumentObject;
    if Assigned( DOMNode) then
      SelectedNode := TXmlNode.Create( DOMNode, nil, Doc);
    result := assigned( SelectedNode)
    end;
    

    Then A much neater solution is ...

    procedure TForm2.btn1Click(Sender: TObject);
    const
      DocumentSource =  'http://softez.pp.ua/gg.xml';
    var
      Doc: IXMLDocument;
      DocOwner: TComponent;
      RowNode, PhraseNode, UrlNode: IXMLNode;
    
      procedure PutLn( const LineFmt: string; const Args: array of const);
      begin
      memo2.Lines.Add( Format( LineFmt, Args))
      end;
    
    begin
    memo2.Clear;
    Doc := CreateXMLDocument( DocOwner);
    Doc.LoadFromFile( DocumentSource);
    for RowNode in XPATHSelect( Doc.DocumentElement, '//row[phrase]') do
      begin
      if not XPATHSelectFirst( RowNode, 'phrase', PhraseNode) then continue;
      PutLn( 'phrase=%s', [PhraseNode.NodeValue]);
      for UrlNode in XPATHSelect( RowNode, 'search_engines/search_engine/se_url') do
        PutLn( 'url=%s', [UrlNode.NodeValue]);
      PutLn('--------------',[])
      end;
    DocOwner.Free;
    end;
    

    This was tested on Delphi 2010 and works a treat.

提交回复
热议问题