Getting Node and Attribute Values

There are several functions to get node and attribute values of a document. All of these functions require a handle to a node. XmlGetNodeValue returns the text value associated with the node. XmlGetNodeTypedValue returns the node value expressed in its defined data type. If you have a look at the sample above, there are child elements with a text value (value1 and value2). XmlGetNodeValue on one of the nodes will return null because value1 and value2 are child nodes of the child elements. To get the test values (value1 and value2) you can either get the first child of one of the child nodes and use XmlGetNodeValue, or you use XmlGetNodeTypedValue on the child nodes directly.

dcltrans
transaction TMain
var
  hDoc, hChild, hTextNode : number;
  sValue : string;
begin
  hDoc      := XmlCreateDocumentFromXml("<root><child>value1</child><child>value2</child></root>");
  hChild    := XmlSelectSingleNode(hDoc, "/root/child[1]");
  hTextNode := XmlGetChildByIndex(hChild, 0);

  // following two functions will return the same value - sValue will contain value1
  XmlGetNodeValue(hTextNode, sValue);
  XmlGetNodeTypedValue(hChild, sValue);
  ...
  XmlFreeHandle(hChild);
  XmlFreeHandle(hTextNode);
  XmlFreeHandle(hDoc);
end TMain;

Attributes can be accessed by name or index. XmlGetAttributeCount returns the number of attributes defined in the passed node. XmlGetAttributeByName returns the value of the attribute identified by name. XmlGetAttributeByIndex returns the value of the attribute identified by the 0 based index.

dcltrans
transaction TMain
var
  hDoc, hChild : number;
  sValue : string;
begin
  hDoc   := XmlCreateDocumentFromXml("<root><child attr1='attrvalue1'>value1</child></root>");
  hChild := XmlSelectSingleNode(hDoc, "/root/child");

  // following two functions will both access attribute attr1 - sValue will contain attrvalue1
  XmlGetAttributeByName(hChild, "attr1", sValue);
  XmlGetAttributeByIndex(hChild, 0, sValue);
  ...
  XmlFreeHandle(hChild);
  XmlFreeHandle(hDoc);
end TMain;