HtmlToXml Lua Reference Documentation

chilkat.HtmlToXml

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.HtmlToXml

Convert HTML into well-formed XML for structured parsing and searching.

Chilkat.HtmlToXml converts HTML into well-formed XML so the content can be parsed, searched, transformed, or processed programmatically with XML tools. It can accept HTML from strings, files, byte arrays, BinData, or StringBuilder, and provides options for dropping selected tags, removing common text-formatting tags, controlling non-breaking spaces, choosing the XML charset, and saving the converted XML to a file.

HTML to XML conversion

Convert real-world HTML into well-formed XML that can be loaded into Chilkat.Xml or other XML processing tools.

Flexible input sources

Supply HTML from strings, files, byte arrays, BinData, or StringBuilder depending on how the content is received or built.

Drop selected tags

Remove specific tag types from the output when the application wants a cleaner XML result focused on relevant content.

Text-formatting cleanup

Drop common formatting tags when the structure matters more than visual presentation.

NBSP handling

Control how non-breaking spaces are represented, converted, removed, or preserved in the XML output.

Charset and file output

Choose the XML charset and write the converted XML directly to a file when a persisted result is needed.

Common pattern: Load or assign the HTML, set any cleanup options such as dropped tags, text-formatting removal, Nbsp behavior, and XmlCharset, then call the conversion method and parse the result as XML. Use HtmlToXml when structured extraction is needed; use HtmlToText when the goal is readable plain text.

Object Creation

local chilkat = require("chilkat")

local html_to_xml = chilkat.HtmlToXml.new()
local html_to_xml = chilkat.HtmlToXml.new()

Creates the object (chilkat.HtmlToXml() does the same). Lua's garbage collector frees it when it is no longer referenced; html_to_xml:dispose() frees it at once, and in Lua 5.4 and later local html_to_xml <close> = chilkat.HtmlToXml.new() frees it when the block ends. Calling a method of a disposed object raises an error.

Errors

A method that fails returns nil followed by the error text, as io.open does: local body, err = html_to_xml:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.

Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.

Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (html_to_xml.verbose_logging = true) and methods use the colon (html_to_xml:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.

Properties

DebugLogFilePath
-- strVal is a string
local strVal = html_to_xml.debug_log_file_path
html_to_xml.debug_log_file_path = strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
DropCustomTags
-- boolVal is a boolean
local boolVal = html_to_xml.drop_custom_tags
html_to_xml.drop_custom_tags = boolVal

Controls whether tags that Chilkat does not recognize as standard HTML elements are omitted from the generated XML.

When to use: Set this property to true when custom or application-specific tags would add noise to the XML structure. Leave it false when those tags are meaningful to the application and should remain represented in the converted output.

top
Html
-- strVal is a string
local strVal = html_to_xml.html
html_to_xml.html = strVal

The HTML source to be converted. Set this property to an HTML string, configure any conversion options, and then call ToXml, Xml, or ToXmlSb.

Assigning this property replaces the current in-memory HTML source. For file-to-file conversion, use ConvertFile. To load HTML from a file, BinData, or StringBuilder, use the corresponding SetHtml* method.

HTML input does not need to already be valid XML. The converter is intended for real-world HTML and repairs markup as needed to produce a well-formed XML document. Chilkat examples show malformed HTML being normalized and ordinary text represented in <text> elements to simplify programmatic extraction.

top
LastErrorHtml
-- strVal is a string (read-only)
local strVal = html_to_xml.last_error_html

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
-- strVal is a string (read-only)
local strVal = html_to_xml.last_error_text

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
-- strVal is a string (read-only)
local strVal = html_to_xml.last_error_xml

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
-- boolVal is a boolean
local boolVal = html_to_xml.last_method_success
html_to_xml.last_method_success = boolVal

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
Nbsp
-- intVal is an integer
local intVal = html_to_xml.nbsp
html_to_xml.nbsp = intVal

Controls how HTML non-breaking-space entities are handled during conversion. The default value is 0.

ValueBehavior for &nbsp;
0Convert to a normal space character (U+0020).
1Convert to the numeric character reference &#160; (U+00A0).
2Drop the non-breaking-space entity.
3Leave the &nbsp; entity unconverted by this option.

This setting applies specifically to the HTML entity &nbsp;; it does not define general whitespace normalization.

top
VerboseLogging
-- boolVal is a boolean
local boolVal = html_to_xml.verbose_logging
html_to_xml.verbose_logging = boolVal

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
-- strVal is a string (read-only)
local strVal = html_to_xml.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top
XmlCharset
-- strVal is a string
local strVal = html_to_xml.xml_charset
html_to_xml.xml_charset = strVal

Specifies the character encoding to identify for the generated XML, such as utf-8 or iso-8859-1. If this property is empty, Chilkat uses the character encoding of the HTML input. Otherwise, the converted XML uses the specified charset.

String versus file output: A string returned by ToXml or Xml is a string in the programming language's native string representation, while the XML declaration reflects XmlCharset. When writing that string to a file, use the same charset so the file bytes agree with the XML declaration. ConvertFile performs the file conversion directly.

top

Methods

ConvertFile
-- in_html_path is a string
-- dest_xml_path is a string
-- ok is true, or nil and err on failure
local ok, err = html_to_xml:convert_file(in_html_path, dest_xml_path)

Converts the HTML file at inHtmlPath to a well-formed XML file at destXmlPath. The output encoding is controlled by XmlCharset; if XmlCharset is empty, Chilkat uses the HTML source encoding.

Returns true on success and false on failure.

Conversion behavior: The input is parsed as HTML rather than XML. Chilkat can normalize malformed or incomplete HTML markup as part of producing a well-formed XML document suitable for subsequent parsing and data extraction.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
DropTagType
-- tag_name is a string
html_to_xml:drop_tag_type(tag_name)

Marks the specified HTML tag type to be dropped from the generated XML. To drop multiple tag types, call this method once for each tag name.

The setting remains in effect for subsequent conversions performed by this HtmlToXml object. Use UndropTagType to reverse the setting for a particular tag type.

top
DropTextFormattingTags
html_to_xml:drop_text_formatting_tags()

Configures the converter to drop common text-formatting tags from the generated XML. The affected tag names are:

b, font, i, u, br, center, em, strong, big, tt, s, small, strike, sub, and sup.

This is useful when the formatting markup is not relevant to later XML parsing and the application wants a simpler structural representation. Call UndropTextFormattingTags to keep these tags instead.

More Information and Examples
top
SetHtmlBd
-- bd is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = html_to_xml:set_html_bd(bd)
Introduced in version 9.5.0.77

Replaces the current HTML source with the complete contents of bd.

The data is copied from the supplied BinData object and becomes the input for a later call to ToXml, Xml, or ToXmlSb. Returns true on success and false on failure.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
SetHtmlFromFile
-- filename is a string
-- ok is true, or nil and err on failure
local ok, err = html_to_xml:set_html_from_file(filename)

Loads HTML from filename and replaces the current Html input with the file contents.

Returns true on success and false if the file cannot be read or the HTML input cannot be loaded. Call ToXml, Xml, or ToXmlSb afterward to perform the conversion.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetHtmlSb
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = html_to_xml:set_html_sb(sb)
Introduced in version 9.5.0.97

Replaces the current HTML source with the complete contents of sb.

The text is copied from the supplied StringBuilder and becomes the input for a later call to ToXml, Xml, or ToXmlSb. Returns true on success and false on failure.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ToXml
-- str is a string, or nil and err on failure
local str, err = html_to_xml:to_xml()

Converts the current Html input to well-formed XML and returns the XML string. The generated XML uses the conversion settings currently configured on this object, including DropCustomTags, dropped tag types, Nbsp, and XmlCharset.

HTML normalization: The input is parsed as HTML, so it does not need to already be well-formed XML. Chilkat can repair incomplete or malformed markup. The generated document is wrapped in a root element, and Chilkat represents ordinary text content in <text> elements to make extraction with XML tools straightforward.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
ToXmlSb
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = html_to_xml:to_xml_sb(sb)
Introduced in version 9.5.0.77

Converts the current Html input to well-formed XML and appends the generated XML to sb.

Returns true on success and false on failure. Because the result is appended, clear the destination StringBuilder first when replacement rather than concatenation is desired.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
UndropTagType
-- tag_name is a string
html_to_xml:undrop_tag_type(tag_name)

Reverses a previous DropTagType setting for the specified tag name so that tag type is no longer intentionally dropped from subsequent XML output.

This affects only the named tag type. Other tag types previously configured to be dropped remain unchanged.

top
UndropTextFormattingTags
html_to_xml:undrop_text_formatting_tags()

Configures the converter to retain the common text-formatting tags that are otherwise dropped by default:

b, font, i, u, br, center, em, strong, big, tt, s, small, strike, sub, and sup.

Call DropTextFormattingTags to return to dropping these formatting tags.

More Information and Examples
top
Xml
-- str is a string, or nil and err on failure
local str, err = html_to_xml:xml()

Equivalent to ToXml. Converts the current Html input to well-formed XML and returns the XML string using the object's current conversion settings.

This method is provided as an alternate name for ToXml; it does not perform a different conversion.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top