2

使用 Delphi 2010,我想使用 TXMLDocument 从以下 XML 示例数据(我省略了不需要的部分)中读取 Location、Smartcard_Location 和 Integrated_Location 的 URL:

<?xml version="1.0" encoding="UTF-8"?>
<PNAgent_Configuration xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
    <Request>
        <Enumeration>
            <Location replaceServerLocation="true" modifiable="true" forcedefault="false" RedirectNow="false">http://2003xa/Citrix/PNAgent/enum.aspx</Location>
            <Smartcard_Location replaceServerLocation="true">https://2003xa/Citrix/PNAgent/smartcard_enum.aspx</Smartcard_Location>
            <Integrated_Location replaceServerLocation="true">http://2003xa/Citrix/PNAgent/integrated_enum.aspx</Integrated_Location>
            <Refresh>
                <OnApplicationStart modifiable="false" forcedefault="true">true</OnApplicationStart>
                <OnResourceRequest modifiable="false" forcedefault="true">false</OnResourceRequest>
                <Poll modifiable="false" forcedefault="true">
                    <Enabled>true</Enabled>
                    <Period>6</Period>
                </Poll>
            </Refresh>
        </Enumeration>
    </Request>
</PNAgent_Configuration>

数据已经从网络服务器加载到 TXMLDcoument 中。解析这些数据并将 URL 转换为字符串值的最简单方法是什么?

4

1 回答 1

4

最简单的方法是使用getElementsByTagName

lx1.Items.Add(XMLDocument1.DOMDocument.getElementsByTagName('Location').item[0].firstChild.nodeValue);
lx1.Items.Add(XMLDocument1.DOMDocument.getElementsByTagName('Smartcard_Location').item[0].firstChild.nodeValue);
lx1.Items.Add(XMLDocument1.DOMDocument.getElementsByTagName('Integrated_Location').item[0].firstChild.nodeValue);

如果您想XPath改用,您可以在 Delphi 的 XmlDom文章中使用 Select Single IXMLNode / TXmlNode Using XPath 中的此功能:

class function TXMLNodeHelper.SelectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
  intfSelect : IDomNodeSelect;
  dnResult : IDomNode;
  intfDocAccess : IXmlDocumentAccess;
  doc: TXmlDocument;
begin
  Result := nil;
  if not Assigned(xnRoot)
    or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
    Exit;

  dnResult := intfSelect.selectNode(nodePath);
  if Assigned(dnResult) then
  begin
    if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
      doc := intfDocAccess.DocumentObject
    else
      doc := nil;
    Result := TXmlNode.Create(dnResult, nil, doc);
  end;
end;

这边走:

lx1.Items.Add(TXMLNodeHelper.SelectNode(XMLDocument1.DocumentElement, '//Location').NodeValue);
于 2012-05-17T10:02:22.063 回答