TChilkatXml Delphi Reference

TChilkatXml

A free non-validating XML parser component with encryption and compression features.

Object Creation

var
obj: IChilkatXml;
...
begin
obj := TChilkatXml.Create(Self).ControlInterface;

Properties

property Cdata: Integer

When True, causes an XML node's content to be encapsulated in a CDATA section.

property Content: WideString

The content of the XML node. It is the text between the open and close tags, not including child nodes. For example:

<tag1>This is the content</tag1>

<tag2><child1>abc</child1><child2>abc</child2>This is the content</tag2>
Because the child nodes are not included, the content of "tag1" and "tag2" are both equal to "This is the content".

Content vs. Children: A common misconception explained.

property ContentInt: Integer

Set/get the content as an integer.

property DocType: WideString

The DOCTYPE declaration (if any) for the XML document.

property Encoding: WideString

This is the encoding attribute in the XML declaration, such as "utf-8" or "iso-8859-1". If not present, this property returns an empty string. This property can be set from any node in the XML document and when set, causes the encoding property to be added to the XML declaration. Setting this property does not cause the document to be converted to a different encoding.

property LastErrorHtml: WideString readonly

Error information in HTML format for the last method called.

property LastErrorText: WideString readonly

Error information in plain-text format for the last method called.

property LastErrorXml: WideString readonly

Error information in XML format for the last method called.

property NumAttributes: Integer readonly

The number of attributes. For example, the following node has 2 attributes:

<tag attr1="value1" attr2="value2"> This is the content</tag>

property NumChildren: Integer readonly

The number of direct child nodes contained under this XML node.

property SortCaseInsensitive: Integer

If true (or 1 for ActiveX), then all Sort* methods use case insensitive sorting.

property Standalone: Integer

This is the standalone attribute in the XML declaration. This property can be set from any node in the XML document. A value of 1 adds a standalone="yes" to the XML declaration:

<?xml ... standalone="yes">

property Tag: WideString

The XML node's tag.

property TreeId: Integer readonly

Each tree (or XML document) has a unique TreeId. This is the ID of the tree, and can be used to determine if two Xml objects belong to the same tree.

property Version: WideString readonly

The version of the XML component, such as "2.0.0".

Methods

function AccumulateTagContent(tag: WideString, skipTags: WideString): WideString;

Accumulates the content of all nodes having a specific tag into a single result string. SkipTags specifies a set of tags whose are to be avoided. The skipTags are formatted as a string of tags delimited by vertical bar characters. All nodes in sub-trees rooted with a tag appearing in skipTags are not included in the result.

Returns a zero-length WideString on failure

function AddAttribute(name: WideString, value: WideString): Integer;

Adds an attribute to the calling node in the XML document. Returns True for success, and False for failure.

function AddAttributeInt(name: WideString, value: Integer): Integer;

Adds an integer attribute to a node.

function AddChildTree(tree: TChilkatXml): Integer;

Adds an entire subtree as a child. If the child was a subtree within another Xml document then the subtree is effectively transferred from one XML document to another.

procedure AddOrUpdateAttribute(name: WideString, value: WideString);

Adds an attribute to an XML node. If an attribute having the specified name already exists, the value is updated.

procedure AddOrUpdateAttributeI(name: WideString, value: Integer);

Adds an integer attribute to an XML node. If an attribute having the specified name already exists, the value is updated.

procedure AddStyleSheet(styleSheet: WideString);

Adds a style sheet declaration to the XML document. The styleSheet should be a string such as:

<?xml-stylesheet href="mystyle.css" title="Compact" type="text/css"?>

procedure AddToAttribute(name: WideString, amount: Integer);

Adds an integer amount to an integer attribute's value. If the attribute does not yet exist, this method behaves the same as AddOrUpdateAttributeI.

procedure AddToChildContent(tag: WideString, amount: Integer);

Adds an integer value to the content of a child node.

procedure AddToContent(amount: Integer);

Adds an integer amount to the node's content.

function AppendToContent(str: WideString): Integer;

Appends text to the content of an XML node

function BEncodeContent(charset: WideString, inData: OleVariant): Integer;

Sets the node's content with 8bit data that is in a specified multibyte character encoding such as utf-8, shift-jis, big5, etc. The data is first B-encoded and the content is set to be the B-encoded string. For example, if called with "Big5"for the charset, you would get a string that looks something like this: "=?Big5?B?pHCtsw==?=". The data is Base64-encoded and stored between the last pair of "?" delimiters. Use the DecodeContent method to retrieve the byte data from a B encoded string.

function ChildContentMatches(tag: WideString, pattern: WideString, caseSensitive: Integer): Integer;

Return true if a child having a specific tag contains content that matches a wildcarded pattern.

function ChilkatPath(pathCmd: WideString): WideString;

Follows a series of commands to navigate through an XML document to return a piece of data or update the caller's reference to a new XML document node.

Note: This method not related to the XPath (XML Path) standard in any way.

The pathCmd is formatted as a series of commands separated by vertical bar characters, and terminated with a return-command:

    command|command|command|...|returnCommand

A command can be any of the following:

  1. TagName -- Navigate to the 1st direct child with the given tag.
  2. TagName[n] -- Navigate to the Nth direct child with the given tag.
  3. .. -- Navigate up to the parent
  4. TagName{Content} -- Navigate to the 1st direct child with the given tag having the exact content.
  5. /T/TagName -- Traverse the XML DOM tree (rooted at the caller) and navigate to the 1st node having the given tag.
  6. /C/TagName,ContentPattern -- Traverse the XML DOM tree (rooted at the caller) and navigate to the 1st node having the given tag with content that matches the ContentPattern. ContentPattern may use one or more asterisk ('*") characters to represent 0 or more of any character.
  7. /C/ContentPattern -- Traverse the XML DOM tree (rooted at the caller) and navigate to the 1st node having any tag with content that matches the ContentPattern. ContentPattern may use one or more asterisk ('*") characters to represent 0 or more of any character.
  8. /A/TagName,AttrName,AttrValuePattern -- Traverse the XML DOM tree (rooted at the caller) and navigate to the 1st node having the given tag, and attribute, with the attribute value that matches the AttrValuePattern. AttrValuePattern may use one or more asterisk ('*") characters to represent 0 or more of any character.
The returnCommand can be any of the following:
  1. * -- Return the Content of the node.
  2. (AttrName) -- Return the value of the given attribute.
  3. $ -- Update the caller's internal reference to be the node (arrived at by following the series of commands). Returns an empty string.

Returns a zero-length WideString on failure

procedure Clear();

Removes all children, attributes, and content from the XML node. Resets the tag name to "unnamed".

function CompressNode(): Integer;

Applies Zip compression to the content of an XML node and replaces the content with base64-encoded compressed data.

function CompressSubtree(): Integer;

Zip compresses the content and entire subtree rooted at the calling XML node and replaces the current content with base64-encoded Zip compressed data. The node and subtree can be restored by calling UnzipTree. Note that the node name and attributes are unaffected.

function ContentMatches(pattern: WideString, caseSensitive: Integer): Integer;

Return true if the node's content matches a wildcarded pattern.

procedure Copy(node: TChilkatXml);

Copies the tag, content, and attributes to the calling node.

procedure CopyRef(copyFromNode: TChilkatXml);

Discards the caller's current internal reference and copies the internal reference from copyFromNode. Effectively updates the caller node to point to the same node in the XML document as copyFromNode.

function DecodeContent(): OleVariant;

Decodes a node's Q or B-encoded content string and returns the byte data.

Returns a zero-length byte array (as an OleVariant) on failure.
An empty array will have a VarArrayHighBound of -1 meaning 0 elements.

function DecodeEntities(str: WideString): WideString;

Utility method to decode HTML entities. It accepts a string containing (potentially) HTML entities and returns a string with the entities decoded.

Returns a zero-length WideString on failure

function DecompressNode(): Integer;

Unzip the content of the XML node replacing it's content with the decompressed data.

function DecompressSubtree(): Integer;

Unzips and recreates the XML node and the entire subtree, restoring it to the state before it was zip compressed.

function DecryptContent(password: WideString): Integer;

Decrypts the content of an XML node that was previously 128-bit AES encrypted with the EncryptContent method.

function DecryptNode(password: WideString): Integer;

Decrypts the content of an XML node that was previously 128-bit AES encrypted with the EncryptContent method.

function EncryptContent(password: WideString): Integer;

Encrypts the content of the calling XML node using 128-bit CBC AES encryption. The base64-encoded encrypted content replaces the original content.

function EncryptNode(password: WideString): Integer;

Encrypts the content of the calling XML node using 128-bit CBC AES encryption.

function ExtractChildByIndex(index: Integer): TChilkatXml;

Removes and returns the Nth child of an XML node. The first child is at index 0.

function ExtractChildByName(tag: WideString, attrName: WideString, attrValue: WideString): TChilkatXml;

Removes and returns the first child node having a tag equal to the tagName. The attributeName and attrValue may be empty or NULL, in which case the first child matching the tag is removed and returned. If attributeName is specified, then the first child having a tag equal to tagName, and an attribute with attributeName is returned. If attrValue is also specified, then only a child having a tag equal to tagName, and an attribute named attributeName, with a value equal to attrValue is returned.

function FindChild(tag: WideString): TChilkatXml;

Returns the child having a specified tag.

function FindChild2(tag: WideString): Integer;

Updates the Xml object's internal reference to point to a child with a specified tag.

function FindNextRecord(tag: WideString, contentPattern: WideString): TChilkatXml;

Returns the next record node where the child with a specific tag matches a wildcarded pattern. This method makes it easy to iterate over high-level records.

function FindOrAddNewChild(tag: WideString): TChilkatXml;

First searches for a child having a tag equal to tagName, and if found, returns it. Otherwise creates a new child, sets the tag equal to tagName, and initializes the Content to empty.

function FirstChild(): TChilkatXml;

Returns the first child. A program can step through the children by calling FirstChild, and then NextSibling repeatedly.

function FirstChild2(): Integer;

Updates the internal reference of the caller to point to its first child.

More information about XML methods ending in "2"

function GetAttrValue(name: WideString): WideString;

Find and return the value of an attribute having a specified name.

Returns a zero-length WideString on failure

function GetAttrValueInt(name: WideString): Integer;

Returns an attribute as an integer.

function GetAttributeName(index: Integer): WideString;

Returns the name of the Nth attribute of an XML node. The first attribute is at index 0.

Returns a zero-length WideString on failure

function GetAttributeValue(index: Integer): WideString;

Returns the value of the Nth attribute of an XML node. The first attribute is at index 0.

Returns a zero-length WideString on failure

function GetAttributeValueInt(index: Integer): Integer;

Returns an attribute as an integer.

function GetBinaryContent(unzipFlag: Integer, decryptFlag: Integer, password: WideString): OleVariant;

Returns binary content of an XML node as a byte array. The content may have been Zip compressed, AES encrypted, or both. Unzip compression and AES decryption flags should be set appropriately.

Returns a zero-length byte array (as an OleVariant) on failure.
An empty array will have a VarArrayHighBound of -1 meaning 0 elements.

function GetChild(index: Integer): TChilkatXml;

Returns the Nth child of an XML node

function GetChild2(index: Integer): Integer;

Updates the calling object's internal reference to the Nth child node.

Returns 1 for success, 0 for failure.

More information about XML methods ending in "2"

function GetChildBoolValue(tag: WideString): Integer;

Returns 0 if the node's content is "0", otherwise returns 1 if the node contains a non-zero integer.

function GetChildContent(tag: WideString): WideString;

Returns the content of a child having a specified tag.

Returns a zero-length WideString on failure

function GetChildContentByIndex(index: Integer): WideString;

Returns the content of the Nth child node.

Returns a zero-length WideString on failure

function GetChildExact(tag: WideString, content: WideString): TChilkatXml;

Returns the child having the exact tag and content.

function GetChildIntValue(tag: WideString): Integer;

Returns the child integer content for a given tag.

function GetChildTag(index: Integer): WideString;

Returns the tag name of the Nth child node.

Returns a zero-length WideString on failure

function GetChildTagByIndex(index: Integer): WideString;

Returns the tag name of the Nth child node.

Returns a zero-length WideString on failure

function GetChildWithAttr(tag: WideString, attrName: WideString, attrValue: WideString): TChilkatXml;

To be documented soon.

function GetChildWithContent(content: WideString): TChilkatXml;

Returns the first child found having the exact content specified.

function GetChildWithTag(tag: WideString): TChilkatXml;

Returns the Xml child object having a tag matching tagName.

function GetNthChildWithTag(tag: WideString, n: Integer): TChilkatXml;

Returns the Nth child having a tag that matches exactly with the tagName. Use the NumChildrenHavingTag method to determine how many children have a particular tag.

function GetNthChildWithTag2(tag: WideString, n: Integer): Integer;

Updates the calling object's internal reference to the Nth child node having a specific tag.

Returns 1 for success, 0 for failure.

function GetParent(): TChilkatXml;

Returns the parent of this XML node, or NULL if the node is the root of the tree.

function GetParent2(): Integer;

Updates the internal reference of the caller to its parent.

More information about XML methods ending in "2"

function GetRoot(): TChilkatXml;

Returns the root node of the XML document

procedure GetRoot2();

Updates the internal reference of the caller to the document root.

More information about XML methods ending in "2"

function GetSelf(): TChilkatXml;

To be documented soon...

function GetXml(): WideString;

Generate the XML text document for the XML tree rooted at this node.

Returns a zero-length WideString on failure

function HasAttrWithValue(name: WideString, value: WideString): Integer;

Returns true if the node contains attribute with the name and value.

function HasAttribute(name: WideString): Integer;

Returns true if the node contains an attribute with the specified name.

function HasChildWithContent(content: WideString): Integer;

Returns true if the node has a direct child node containing the exact content string specified.

function HasChildWithTag(tag: WideString): Integer;

Returns true (1 for ActiveX) if the node has a direct child with a given tag.

function HasChildWithTagAndContent(tag: WideString, content: WideString): Integer;

Returns true if the node contains a direct child having the exact tag and content specified.

procedure InsertChildTreeAfter(index: Integer, tree: TChilkatXml);

Adds an entire subtree as a child. If the child was a subtree within another Xml document then the subtree is effectively transferred from one XML document to another. The child tree is inserted in a position after the Nth child (of the calling node).

procedure InsertChildTreeBefore(index: Integer, tree: TChilkatXml);

Adds an entire subtree as a child. If the child was a subtree within another Xml document then the subtree is effectively transferred from one XML document to another. The child tree is inserted in a position before the Nth child (of the calling node).

function LastChild(): TChilkatXml;

Returns the last Xml child node. A node's children can be enumerated by calling LastChild and then repeatedly calling PreviousSibling, until a NULL is returned.

function LastChild2(): Integer;

Updates the internal reference of the caller to its last child.

function LoadXml(xmlData: WideString): Integer;

Loads an XML document from a memory buffer and returns True if successful. The contents of the calling node are replaced with the root node of the XML document loaded.

function LoadXml2(xmlData: WideString, autoTrim: Integer): Integer;

Same as LoadXml, but an additional argument controls whether or not leading/trailing whitespace is auto-trimmed from each node's content.

Returns 1 for success, 0 for failure.

function LoadXmlFile(fileName: WideString): Integer;

Loads an XML document from a file and returns True if successful. The contents of the calling node are replaced with the root node of the XML document loaded.

function LoadXmlFile2(fileName: WideString, autoTrim: Integer): Integer;

Same as LoadXmlFile, but an additional argument controls whether or not leading/trailing whitespace is auto-trimmed from each node's content.

Returns 1 for success, 0 for failure.

function LoadXmlV(xmlDoc: OleVariant): Integer;

Loads XML from a byte array containing the XML document.

function NewChild(tag: WideString, content: WideString): TChilkatXml;

Creates a new child having tag and content. The new child is created even if a child with a tag equal to tagName already exists. (Use FindOrAddNewChild to prevent creating children having the same tags.)

procedure NewChild2(tag: WideString, content: WideString);

Creates a new child but does not return the node that is created.

function NewChildAfter(index: Integer, tag: WideString, content: WideString): TChilkatXml;

Inserts a new child in a position after the Nth child node.

function NewChildBefore(index: Integer, tag: WideString, content: WideString): TChilkatXml;

Inserts a new child in a position before the Nth child node.

procedure NewChildInt2(tag: WideString, value: Integer);

Inserts a new child having an integer for content.

function NextSibling(): TChilkatXml;

Returns the nodes next sibling, or NULL if there are no more.

function NextSibling2(): Integer;

Updates the internal reference of the caller to its next sibling.

More information about XML methods ending in "2"

function NumChildrenHavingTag(tag: WideString): Integer;

Returns the number of children having a specific tag name.

function PreviousSibling(): TChilkatXml;

Returns the Xml object that is the node's previous sibling, or NULL if there are no more.

function PreviousSibling2(): Integer;

Updates the internal reference of the caller to its previous sibling.

More information about XML methods ending in "2"

function QEncodeContent(charset: WideString, inData: OleVariant): Integer;

Sets the node's content with 8bit data that is in a specified multibyte character encoding such as utf-8, shift-jis, big5, etc. The data is first Q-encoded and the content is set to be the Q-encoded string. For example, if called with "gb2312"for the charset, you would get a string that looks something like this: "=?gb2312?Q?=C5=B5=BB=F9?=". Character that are not 7bit are represented as "=XX" where XX is the hexidecimal value of the byte. Use the DecodeContent method to retrieve the byte data from a Q encoded string.

function RemoveAllAttributes(): Integer;

Removes all attributes from an XML node. Should always return True.

procedure RemoveAllChildren();

Removes all children from the calling node.

function RemoveAttribute(name: WideString): Integer;

Removes an attribute by name from and XML node.

procedure RemoveChild(tag: WideString);

Removes all direct children with a given tag.

procedure RemoveChildByIndex(index: Integer);

Removes the Nth child from the calling node.

procedure RemoveChildWithContent(content: WideString);

Removes all children having the exact content specified.

procedure RemoveFromTree();

Removes the calling object and its sub-tree from the XML document making it the root of its own tree.

function SaveBinaryContent(filename: WideString, unzipFlag: Integer, decryptFlag: Integer, password: WideString): Integer;

Saves a node's binary content to a file.

function SaveLastError(filename: WideString): Integer;

Saves the last error information to an XML formatted file.

function SaveXml(fileName: WideString): Integer;

Generates XML representing the tree or subtree rooted at this node and writes it to a file.

function SearchAllForContent(after: TChilkatXml, contentPattern: WideString): TChilkatXml;

Returns the first node whose content matches the contentPattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the sub-tree rooted at the caller, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

function SearchAllForContent2(after: TChilkatXml, contentPattern: WideString): Integer;

Same as SearchAllForContent except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

function SearchForAttribute(after: TChilkatXml, tag: WideString, attr: WideString, valuePattern: WideString): TChilkatXml;

Returns the first node having a tag equal to tagName, and an attribute named attrName whose value matches valuePattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the sub-tree rooted at the caller, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

function SearchForAttribute2(after: TChilkatXml, tag: WideString, attr: WideString, valuePattern: WideString): Integer;

Same as SearchForAttribute except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

function SearchForContent(after: TChilkatXml, tag: WideString, contentPattern: WideString): TChilkatXml;

Returns the first node having a tag equal to tagName whose content matches contentPattern, which is a case-sensitive string that can use any number of '*'s to represent 0 or more occurances of any character. The search is breadth-first over the sub-tree rooted at the caller, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

function SearchForContent2(after: TChilkatXml, tag: WideString, contentPattern: WideString): Integer;

Same as SearchForContent except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

function SearchForTag(after: TChilkatXml, tag: WideString): TChilkatXml;

Returns the first node having a tag equal to tagName. The search is breadth-first over the sub-tree rooted at the caller, and the previous node returned can be passed to the next call as afterNode to continue the search after that node.

function SearchForTag2(after: TChilkatXml, tag: WideString): Integer;

Same as SearchForTag except the internal reference of the caller is updated to point to the search result (instead of returning a new object).

function SetBinaryContent(inData: OleVariant, zipFlag: Integer, encryptFlag: Integer, password: WideString): Integer;

Sets the node's content to a block of binary data with optional Zip compression and/or AES encryption. The binary data is automatically converted to base64 format whenever XML text is generated. If the zipFlag is True, the data is first compressed. If the encryptFlag is True, the data is AES encrypted using the Rijndael 128-bit symmetric-encryption algorithm.

function SetBinaryContentFromFile(filename: WideString, zipFlag: Integer, encryptFlag: Integer, password: WideString): Integer;

Sets the node's content with binary (or text) data from a file. The file contents can be Zip compressed and/or encrypted, and the result is base-64 encoded.

procedure SortByAttribute(attrName: WideString, ascending: Integer);

Sorts the direct child nodes by the value of a specified attribute.

procedure SortByAttributeInt(attrName: WideString, ascending: Integer);

Sorts the direct child nodes by the value of a specified attribute interpreted as an integer (not lexicographically as strings).

procedure SortByContent(ascending: Integer);

Sorts the direct child nodes by content.

procedure SortByTag(ascending: Integer);

Sorts the direct child nodes by tag.

procedure SortRecordsByAttribute(sortTag: WideString, attrName: WideString, ascending: Integer);

Sorts the direct child nodes by the content of an attribute in the grandchild nodes.

procedure SortRecordsByContent(sortTag: WideString, ascending: Integer);

Sorts the direct child nodes by the content of the grandchild nodes.

procedure SortRecordsByContentInt(sortTag: WideString, ascending: Integer);

Sorts the direct child nodes by the content of the grandchild nodes. For sorting purposes, the content is interpreted as an integer (not lexicographically as for strings).

function SwapNode(node: TChilkatXml): Integer;

Swaps another node's tag, content, and attributes with this one.

function SwapTree(tree: TChilkatXml): Integer;

Swaps another node's tag, content, attributes, and children with this one.

function TagContent(tag: WideString): WideString;

To be documented soon...

function TagEquals(tag: WideString): Integer;

Returns 1 if the node's tag equals the specified string.

function UnzipContent(): Integer;

Unzip the content of the XML node replacing it's content with the decompressed data.

function UnzipTree(): Integer;

Unzips and recreates the XML node and the entire subtree, restoring it to the state before it was zip compressed.

function UpdateAttribute(attrName: WideString, attrValue: WideString): Integer;

Adds an attribute to the node if it doesn't already exist. Otherwise it updates the existing attribute with the new value.

function UpdateAttributeInt(attrName: WideString, value: Integer): Integer;

Updates an attribute value. (Call UpdateAttribute if the attribute value is a string.)

procedure UpdateChildContent(tag: WideString, value: WideString);

Replaces the content of a child node.

procedure UpdateChildContentInt(tag: WideString, value: Integer);

Replaces the content of a child node where the content is an integer.

function ZipContent(): Integer;

Applies Zip compression to the content of an XML node and replaces the content with base64-encoded compressed data.

function ZipTree(): Integer;

Zip compresses the content and entire subtree rooted at the calling XML node and replaces the current content with base64-encoded Zip compressed data. The node and subtree can be restored by calling UnzipTree. Note that the node name and attributes are unaffected.