LoadDomDocumentXPath

Ukázka načtení DOM dokumentu z XML souboru pomocí XPath

Popis

Obsah načítaného XML souboru je nasledující
<?xml version="1.0"?>
<request>
  <method>GET</method>
  <path>/request</path>
  <query>param=value</query>
  <headers>
    <header name="Content-Type">text/plain</header>
    <header name="Accept">*/*</header>
    <header name="Cache-Control">no-cache</header>
    <header name="Host">localhost:8080</header>
    <header name="Accept-Encoding">gzip, deflate, br</header>
    <header name="Connection">keep-alive</header>
    <header name="Content-Length">12</header>
  </headers>
  <content>SGVsbG8gV29ybGQh</content>
</request>
Skript načte DOM dokument z XML souboru a pomocí XPath vypíše následující.
  1. Obsah elementů "method" a "path"
  2. Obsah elementu "header" , který má hodnotou "Host" v atributu "name"
declare function LoadBase64FromFile dll "ActualDocument" (string): string

declare function DomCreateDocument dll "ScriptEx" (integer, string, string, integer): integer
declare procedure DomFree dll "ScriptEx" (var integer)

declare function DomGetIntegerProp dll "ScriptEx" (integer, string): integer
declare function DomGetStringProp dll "ScriptEx" (integer, string): string
declare procedure DomSetStringProp dll "ScriptEx" (integer, string, string)

declare function DomEvaluate dll "ScriptEx" (integer, string, integer, integer, integer, integer): integer
declare function DomSnapshotItem dll "ScriptEx" (integer, integer): integer

script LoadDomDocumentXPath(): boolean
var
  Base64: string
  TheDocument: integer
  XPathResult: integer
  I: integer
  Node: integer
  S: string
  HostNode: integer
  Host: string
begin
  Base64 := LoadBase64FromFile("c:\Temp\Request.xml")

  TheDocument := DomCreateDocument(0, "", "", 0)
  DomSetStringProp(TheDocument, "Base64", Base64)

  XPathResult := DomEvaluate(TheDocument, "/request/method/node() | /request/path/node()", 0, 0, 0, 0)

  for I := 0 to DomGetIntegerProp(XPathResult, "SnapshotLength") - 1 do
    Node := DomSnapshotItem(XPathResult, I)
    S := DomGetStringProp(Node, "NodeValue")

    write(S)
  end

  XPathResult := DomEvaluate(TheDocument, "/request/headers/header[@name=""Host""]/node()", 0, 0, 0, XPathResult)
  HostNode := DomGetIntegerProp(XPathResult, "SingleNodeValue")
  Host := DomGetStringProp(HostNode, "NodeValue")

  write("Host: " + Host)

  DomFree(TheDocument)

  result := true
end