Monday, March 28, 2011

XPath in Delphi7?

What is the best way of searching XML documents using XPath in Delphi7?

From stackoverflow
  • It depends on the size of the xml document. But I have good experience with both MSXML and its Saxon counterpart.

    If the xml is large (>50 MB) or the queries are heavy (use some // to make your system crawl) expect some delay time. But else it is perfectly doable.

    In later versions, msxml is available as a unit. In version 7 you need to install a type library:

    • Go to Project\Import type library
    • Select Microsoft XML, (the highest version you can find)
    • Select Create unit to create MSXML_TLB

    You can use MSXML_TLB to read xml documents, use xslt and perform xpath queries:

    var
      doc  : IXMLDomDocument2;
      list : IXMLDomNodeList;
      node : IXMLDomNode;
      i    : Integer;
    
    begin
      doc := CoDOMDocument.Create;
      doc.load(xmlfilename); 
    
      list := doc.selectNodes(xpath);
      for i := 0 to list.length-1 do begin
        node := list.item[i];
        if node<>nil then
          Memo1.Lines.Add(node.nodeName);
      end;
    end;
    
    Ricardo Acras : Thanks for your answere... How do I "install" MSXML in Delphi?
    Gamecat : I added an example in the answer. Hope it works.
  • Hi,

    When I have to deal with XML files in Delphi I always use OmniXML, a component I've been using for years. I'm totally happy with it, mainly because it's light, easy to use and free.

    And it works with XPath in a easy way also. It's worth a try, I hope it helps you also.

0 comments:

Post a Comment