德尔福 XE &OmniXML:使用 SelectNode()? [英] Delphi XE & OmniXML: Using SelectNode()?

查看:31
本文介绍了德尔福 XE &OmniXML:使用 SelectNode()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下 XML 片段作为我正在使用最新 OmniXML 快照处理的较大 XML 文件的一部分:

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>

我目前有一个指向 节点的变量,并且想要选择 节点的内容.我正在使用以下代码,但是 Node2 即将出现 Nil...

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;

为什么 Node2 Nil?根据OmniXMLUtils.pas 中的帮助,SelectNode 将选择单个节点可能比低一级.肯定有一个 节点.即使尝试通过 XPathSelect(Node1, 'Name'); 查找节点也会返回一个空列表.我以某种方式使用 OmniXML 是错误的吗?是否可以在不先选择

节点的情况下到达 节点?

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?

推荐答案

SelectNode 工作正常,如果你把双斜线字符放在前面:

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;

如果您愿意,XPath 也可以正常工作:

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;

这篇关于德尔福 XE &amp;amp;OmniXML:使用 SelectNode()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆