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>

データはすでに Web サーバーから 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);

代わりに使用したい場合は、Delphi の XmlDom記事でXPath を使用して単一の IXMLNode / TXmlNode を選択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 に答える