afrazier
afrazier

Reputation: 4902

Delphi XE & OmniXML: Using SelectNode()?

I've got the following XML snippet as part of a larger XML file that I'm processing using the latest OmniXML snapshot:

<OrderRequestHeader>
<!-- snipped XML bits here -->
<ShipTo>                                                        
    <Address addressID="">                                      
        <Name xml:lang="en">SOME COMPANY</Name>        
        <PostalAddress name="default">                          
            <DeliverTo>John Doe</DeliverTo>                  
            <Street>123 Any St</Street>                  
            <City>Nowhere</City>                              
            <State>AK</State>                                   
            <PostalCode>99999</PostalCode>                      
            <Country isoCountryCode="US">United States</Country>
        </PostalAddress>                                        
        <Email/>                                                
        <Phone>                                                 
            <TelephoneNumber>                                   
                <CountryCode isoCountryCode=""/>                
                <AreaOrCityCode/>                               
                <Number></Number>                               
            </TelephoneNumber>                                  
        </Phone>                                                
    </Address>                                                  
</ShipTo>
<!-- more XML stuff follows -->
</OrderRequestHeader>

I've currently got a variable pointing to the <ShipTo> node, and want to select the contents of the <Name> node. I'm using the following code, but Node2 is coming up Nil...

procedure ProcessXML;
var
    Node1, Node2: IXMLNode;

begin
    Node1 := FindNode(OrderHeader, 'ShipTo');
    // the above is working.  Node points to the <ShipTo> node
    Node2 := SelectNode(Node1, 'Name');
    // the above line doesn't work.  Node2 is Nil
end;

Why is Node2 Nil? According to the help in OmniXMLUtils.pas, SelectNode will select a single node possibly more than one level below. There's definitely a <Name> node. Even trying to find the node via XPathSelect(Node1, 'Name'); returns an empty list. Am I using OmniXML wrong somehow? Is it possible to get to the <Name> node without first selecting the <Address> node?

Upvotes: 3

Views: 2566

Answers (2)

Ken White
Ken White

Reputation: 125620

SelectNode works fine, if you put double-slash characters in front:

var
  FXMLDocument: IXMLDocument;

// Somewhere along the line
  FXMLDocument := CreateXMLDocument
  XMLLoadFromFile(FXMLDocument, 'WhateverFile.xml');
  // or XMLLoadFromAnsiString(FXMLDocument, SomeStringVar);


var
  QryNode, Node: IXMLNode;
begin
  QryNode := FXMLDocument.DocumentElement;
  Node := SelectNode(QryNode, 'ShipTo');
  if Assigned(Node) then
  begin
    QryNode := SelectNode(Node, '//Name');
    if Assigned(QryNode) then
      ShowMessage('ShipTo Name is ' + QryNode.FirstChild.Text)
    else
      ShowMessage('Name not found');
  end;
end;

If you prefer, XPath works fine as well:

implementation

var
  FXMLDocument: IXMLDocument;

// Somewhere along the line
  FXMLDocument := CreateXMLDocument
  XMLLoadFromFile(FXMLDocument, 'WhateverFile.xml');
  // or XMLLoadFromAnsiString(FXMLDocument, SomeStringVar);

function GetShipTo: string;
var
  QryNode: IXMLNode;
  Node: IXMLNode;
  NodeList: IXMLNodeList;
begin
  Result := '';
  QryNode := FXMLDocument.DocumentElement;  

  // The following also work:
  // '//Address/Name'
  // '//Name'
  NodeList := XPathSelect(QryNode, '//ShipTo/Address/Name');
  if NodeList.Length > 0 then
    QryNode := NodeList.Item[0]
  else
    QryNode := nil;
  if Assigned(QryNode) then
    Result := QryNode.FirstChild.Text; // Now has 'SOME COMPANY'
end;

Upvotes: 2

afrazier
afrazier

Reputation: 4902

I figured out what I was doing wrong: I have to specify the entire relative path, not just the name of the grandchild node I want.

From my ProcessXML example above, I have to fill in Node2 like so:

Node2 := SelectNode(Node, 'Address/Name');

Using XPath, I'd have to find it via XPathSelect(Node, 'Address/Name');

Upvotes: 2

Related Questions