XML Component

Properties   Methods   Events   Configuration Settings   Errors  

The XML component can be used to both parse and create XML documents.

Syntax

ipworks.Xml

Remarks

The XML component can operate as either a parser of writer of XML.

Parsing XML

The XML component parses XML documents and verifies that they are well-formed. The results are provided through a set of events complying with the SAX2 specification.

In addition, the document structure may be queried through an XPath mechanism that supports a subset of the XPath specification.

The parser is optimized for read applications, with a very fast engine that builds internal DOM structures with close to zero heap allocations. Additionally, BuildDOM can be set to False which reduces the overhead of creating the DOM and offers a fast forward-only parsing implementation which fires events to provide the parsed data.

When parsing a document events will fire to provide information about the parsed data. After Parse returns the document may be navigated by setting XPath if BuildDOM is True (default). If BuildDOM is False parsed data is only accessible through the events.

Events are fired only when qualifying conditions (such as, for example, the beginning of a new element) are met. In the meantime, text will be buffered internally. The following events will fire during parsing:

If BuildDOM is True (default), XPath may be set after this method returns. XPath implements a subset of the XML XPath specification, allowing you to point to specific elements in the XML documents.

The path is a series of one or more element accessors separated by '/'. The path can be absolute (starting with '/') or relative to the current XPath location.

The following are possible values for an element accessor:

'name'A particular element name
name[i]The i-th subelement of the current element with the given name
[i]The i-th subelement of the current element
[last()]The last subelement of the current element
[last()-i]The subelement located at the last location minus i in the current element
name[@attrname="attrvalue"]The subelement containing a particular value for a given attribute (supports single AND double quotes)
..The parent of the current element

When XPath is set to a valid path, XElement points to the name of the element, with XParent, XNamespace, XPrefix, XChildren , and XText providing other properties of the element. The attributes of the current element are provided in the XAttributes collection.

BuildDOM must be set to True prior to parsing the document for the XPath functionality to be available.

Example (Setting XPath):

Document rootXML.XPath = "/"
Specific ElementXML.XPath = "/root/SubElement1/SubElement2/"
i-th ChildXML.XPath = "/root/SubElement1[i]"

Input Properties

The component will determine the source of the input based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops.

If parsing multiple documents call Reset between documents to reset the parser.

An additional "relaxed" mode allows for lexical parsing of non-XML documents (e.g. HTML). This is enabled by setting Validate to False. In this case, events will be fired as elements, entities, etc. are encountered, but the structure of the document will not be checked for "well-formedness", and the internal DOM structure will not be built.

Writing XML

To use the component first decide whether or not to write to file, a stream, or to OutputData.

Output Properties

The component will determine the destination of the output based on which properties are set.

The order in which the output properties are checked is as follows:

To begin writing the XML document first optionally set XMLDeclaration. If this is not set the component will use a default XML declaration at the beginning of the document.

Next begin adding elements to your document. Calling StartElement will open an element with the specified name. To create a nested structure continue calling StartElement to open more child elements. To write a value within an element call PutString. To close the element that was last opened call EndElement. Each time EndElement is called the element at the current level is closed. Alternatively, calling PutElement will write the element specified with the value specified and will also close the element.

To write an attribute of the current element, after calling StartElement call PutAttr. Call PutAttr multiple times to add multiple attributes.

Writing comments or CDATA can be done at any time with the PutComment and PutCData methods.

To close your XML document call Save. You can call Save from any location and it will close any remaining open elements automatically.

Property List


The following is the full list of the properties of the component with short descriptions. Click on the links for further details.

BuildDOMWhen True, an internal object model of the XML document is created.
InputDataThe XML data to parse.
InputFileThe file to process.
NamespacesCollection of namespaces in the current namespace stack.
OutputDataThe output XML after processing.
OutputFileThe path to a local file where the output will be written.
OverwriteIndicates whether or not the component should overwrite files.
ValidateWhen True, the parser checks that the document consists of well-formed XML.
XAttributesA collection of attributes of the current element.
XChildrenCollection of child elements of the currently selected XML element.
XCommentsA collection of comments of the current element.
XElementThe name of the current element.
XMLDeclarationSpecifies details of the XML declaration.
XNamespaceThe namespace of the current element.
XParentThe parent of the current element.
XPathProvides a way to point to a specific element in the document.
XPrefixThe prefix of the current element.
XSubTreeA snapshot of the current element in the document.
XTextThe text of the current element.

Method List


The following is the full list of the methods of the component with short descriptions. Click on the links for further details.

ConfigSets or retrieves a configuration setting.
EndElementWrites the closing tag of an open XML element.
FlushFlushes the parser and checks its end state.
GetAttrReturns the value of the specified attribute.
HasXPathDetermines whether a specific element exists in the document.
LoadDOMLoads the DOM from a file.
LoadSchemaLoad the XML schema.
ParseThis method parses the specified XML data.
PutAttrWrites an XML attribute.
PutCDataWrites an XML CDATA block.
PutCommentWrites an XML comment block.
PutElementWrites a simple XML element with a value.
PutRawWrites a raw XML fragment.
PutStringWrites text inside an XML element.
RemoveAttrRemove a attribute.
RemoveChildrenRemoves the children of the elment as the specified XPath.
RemoveElementRemoves the elment as the specified XPath.
ResetResets the parser.
SaveCloses the component writing stream.
SaveDOMSaves the DOM to a file.
SetInputStreamSets the stream from which the component will read data to parse.
SetOutputStreamThe stream to which the component will write the XML.
StartElementWrites the opening tag of an XML element.
TryXPathNavigates to the specified XPath if it exists.

Event List


The following is the full list of the events fired by the component with short descriptions. Click on the links for further details.

CharactersFired for plain text segments of the input stream.
CommentFired when a comment section is encountered.
EndElementFired when an end-element tag is encountered.
EndPrefixMappingFired when leaving the scope of a namespace declaration.
ErrorInformation about errors during data delivery.
EvalEntityFired every time an entity needs to be evaluated.
IgnorableWhitespaceFired when a section of ignorable whitespace is encountered.
MetaFired when a meta section is encountered.
PIFired when a processing instruction section is encountered.
SpecialSectionFired when a special section is encountered.
StartElementFired when a begin-element tag is encountered in the document.
StartPrefixMappingFired when entering the scope of a namespace declaration.
XMLFires as XML is written.

Configuration Settings


The following is a list of configuration settings for the component with short descriptions. Click on the links for further details.

CacheContentIf true, the original XML is saved in a buffer.
CharsetSpecifies the charset used when encoding data.
CloseInputStreamAfterProcessDetermines whether or not the input stream is closed after processing.
CloseOutputStreamAfterProcessDetermines whether or not the output stream is closed after processing.
EOLThe characters to use for separating lines.
ErrorOnEmptyAttrIf true, passing an invalid attribute to the Attr method will throw an exception.
ExtraNameCharsExtra characters for the parser to consider as name characters.
ExtraSpaceCharsExtra characters for the parser to consider as white space.
FlushOnEOLIf set, the parser flushes its text buffer after every line of text.
IgnoreBadAttributePrefixesIf true, bad (unknown) attribute prefixes are ignored.
IgnoreBadElementPrefixesIf true, bad (unknown) element prefixes are ignored.
IncludeElementPrefixWhether to include the prefix in the element name.
IncludeXMLDeclarationWhether to include the XML declaration when writing XML.
IndentThe characters to use for each indentation level.
OffsetCurrent offset of the document being parsed.
PreserveWhitespaceIf true, leading and trailing whitespace in element text is preserved.
QuoteCharQuote character to use for attribute values.
StringProcessingOptionsDefines options to use when processing string values.
BuildInfoInformation about the product's build.
GUIAvailableTells the component whether or not a message loop is available for processing events.
LicenseInfoInformation about the current license.
UseDaemonThreadsWhether threads created by the component are daemon threads.
UseInternalSecurityAPITells the component whether or not to use the system security libraries or an internal implementation.

Copyright (c) 2021 /n software inc. - All rights reserved.
IPWorks 2020 Kotlin Edition - Version 20.0 [Build 7941]