XML Class

Properties   Methods   Events   Configuration Settings   Errors  

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

Class Name

IPWorks_XML

Procedural Interface

 ipworks_xml_open();
 ipworks_xml_close($res);
 ipworks_xml_register_callback($res, $id, $function);
 ipworks_xml_get_last_error($res);
 ipworks_xml_get_last_error_code($res);
 ipworks_xml_set($res, $id, $index, $value);
 ipworks_xml_get($res, $id, $index);
 ipworks_xml_do_config($res, $configurationstring);
 ipworks_xml_do_endelement($res);
 ipworks_xml_do_flush($res);
 ipworks_xml_do_getattr($res, $attrname);
 ipworks_xml_do_hasxpath($res, $xpath);
 ipworks_xml_do_loaddom($res, $filename);
 ipworks_xml_do_loadschema($res, $schema);
 ipworks_xml_do_parse($res);
 ipworks_xml_do_putattr($res, $name, $namespaceuri, $value);
 ipworks_xml_do_putcdata($res, $text);
 ipworks_xml_do_putcomment($res, $text);
 ipworks_xml_do_putelement($res, $name, $namespaceuri, $value);
 ipworks_xml_do_putraw($res, $text);
 ipworks_xml_do_putstring($res, $text);
 ipworks_xml_do_removeattr($res, $attrname);
 ipworks_xml_do_removechildren($res);
 ipworks_xml_do_removeelement($res);
 ipworks_xml_do_reset($res);
 ipworks_xml_do_save($res);
 ipworks_xml_do_savedom($res, $filename);
 ipworks_xml_do_startelement($res, $name, $namespaceuri);
 ipworks_xml_do_tryxpath($res, $xpath);

Remarks

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

Parsing XML

The XML class 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, XChildName, XChildNamespace, XChildPrefix, XChildXText, and XText providing other properties of the element. The attributes of the current element are provided in the AttrName, AttrNamespace, AttrPrefix, and AttrValue properties.

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 class 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 class first decide whether or not to write to file, or to OutputData.

Output Properties

The class 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:

  • OutputFile
  • OutputData: The output data is written to this property if no other destination is specified.

To begin writing the XML document first optionally set XMLDeclaration. If this is not set the class 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 class 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.
NamespaceCountThe number of records in the Namespace arrays.
NamespacePrefixThe Prefix for the Namespace .
NamespaceURINamespace URI associated with the corresponding Prefix .
OutputDataThe output XML after processing.
OutputFileThe path to a local file where the output will be written.
OverwriteIndicates whether or not the class should overwrite files.
ValidateWhen True, the parser checks that the document consists of well-formed XML.
AttrCountThe number of records in the Attr arrays.
AttrNameThe Name provides the local name (without prefix) of the attribute.
AttrNamespaceAttribute namespace.
AttrPrefixAttribute prefix (if any).
AttrValueAttribute value.
XChildCountThe number of records in the XChild arrays.
XChildNameThe Name property provides the local name (without prefix) of the element.
XChildNamespaceNamespace of the element.
XChildPrefixPrefix of the element (if any).
XChildXTextThe inner text of the element.
XCommentCountThe number of records in the XComment arrays.
XCommentTextThis property holds the comment text.
XElementThe name of the current element.
XMLDeclarationEncodingThis property specifies the XML encoding to use.
XMLDeclarationStandaloneThis property indicates whether the standalone attribute is present in the declaration with a value of true.
XMLDeclarationVersionThis property specifies the XML version.
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 class 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 class writing stream.
SaveDOMSaves the DOM to a file.
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 class 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 class 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.
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.
CodePageThe system code page used for Unicode to Multibyte translations.
LicenseInfoInformation about the current license.
ProcessIdleEventsWhether the class uses its internal event loop to process events when the main thread is idle.
SelectWaitMillisThe length of time in milliseconds the class will wait when DoEvents is called if there are no events to process.
UseInternalSecurityAPITells the class whether or not to use the system security libraries or an internal implementation.

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks 2020 PHP Edition - Version 20.0 [Build 8307]