HtmlToXml Chilkat2 Perl Reference Documentation
Chilkat2::HtmlToXml
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.HtmlToXml
Convert real-world HTML into well-formed XML that can be loaded into
Supply HTML from strings, files, byte arrays,
Remove specific tag types from the output when the application wants a
cleaner XML result focused on relevant content.
Drop common formatting tags when the structure matters more than visual
presentation.
Control how non-breaking spaces are represented, converted, removed, or
preserved in the XML output.
Choose the XML charset and write the converted XML directly to a file
when a persisted result is needed.
For an extended overview, see
HtmlToXml Class Overview.
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
Chilkat.Xml or other XML processing tools.
Flexible input sources
BinData, or
StringBuilder depending on how the content is received or
built.
Drop selected tags
Text-formatting cleanup
NBSP handling
Charset and file output
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
use Chilkat2;
my $htmlToXml = Chilkat2::HtmlToXml->new;
# or set properties (and RaiseError) at creation:
# my $htmlToXml = Chilkat2::HtmlToXml->new(VerboseLogging => 1, RaiseError => 1);Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/HtmlToXml.pm file holds only this class's documentation (perldoc Chilkat2::HtmlToXml).
Perl frees the object when its last reference goes (undef $htmlToXml, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::HtmlToXml';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.
Errors
A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::HtmlToXml'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.
Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.
Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.
Properties
DebugLogFilePath
my $strVal = $htmlToXml->DebugLogFilePath;
$htmlToXml->DebugLogFilePath($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.
DropCustomTags
my $boolVal = $htmlToXml->DropCustomTags;
$htmlToXml->DropCustomTags($boolVal);
Controls whether tags that Chilkat does not recognize as standard HTML elements are omitted from the generated XML.
1 when custom or application-specific tags would add noise to the XML structure. Leave it 0 when those tags are meaningful to the application and should remain represented in the converted output.Html
my $strVal = $htmlToXml->Html;
$htmlToXml->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.
<text> elements to simplify programmatic extraction.LastErrorHtml
my $strVal = $htmlToXml->LastErrorHtml;
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.
topLastErrorText
my $strVal = $htmlToXml->LastErrorText;
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.
LastErrorXml
my $strVal = $htmlToXml->LastErrorXml;
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.
topLastMethodSuccess
my $boolVal = $htmlToXml->LastMethodSuccess;
$htmlToXml->LastMethodSuccess($boolVal);
Indicates the success or failure of the most recent method call: 1 means success, 0 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.
Nbsp
my $intVal = $htmlToXml->Nbsp;
$htmlToXml->Nbsp($intVal);
Controls how HTML non-breaking-space entities are handled during conversion. The default value is 0.
| Value | Behavior for |
|---|---|
0 | Convert to a normal space character (U+0020). |
1 | Convert to the numeric character reference   (U+00A0). |
2 | Drop the non-breaking-space entity. |
3 | Leave the entity unconverted by this option. |
This setting applies specifically to the HTML entity ; it does not define general whitespace normalization.
VerboseLogging
my $boolVal = $htmlToXml->VerboseLogging;
$htmlToXml->VerboseLogging($boolVal);
If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
my $strVal = $htmlToXml->Version;
XmlCharset
my $strVal = $htmlToXml->XmlCharset;
$htmlToXml->XmlCharset($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.
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.Methods
ConvertFile
# $destXmlPath is a string
# $success is true or false
my $success = $htmlToXml->ConvertFile($inHtmlPath, $destXmlPath);
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 1 on success and 0 on failure.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
DropTagType
$htmlToXml->DropTagType($tagName);
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.
Returns the object, so calls can be chained.
topDropTextFormattingTags
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.
Returns the object, so calls can be chained.
SetHtmlBd
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 1 on success and 0 on failure.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SetHtmlFromFile
# $success is true or false
my $success = $htmlToXml->SetHtmlFromFile($filename);
Loads HTML from filename and replaces the current Html input with the file contents.
Returns 1 on success and 0 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, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SetHtmlSb
# $success is true or false
my $success = $htmlToXml->SetHtmlSb($sb);
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 1 on success and 0 on failure.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
ToXml
my $str = $htmlToXml->ToXml();
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.
<text> elements to make extraction with XML tools straightforward.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
ToXmlSb
# $success is true or false
my $success = $htmlToXml->ToXmlSb($sb);
Converts the current Html input to well-formed XML and appends the generated XML to sb.
Returns 1 on success and 0 on failure. Because the result is appended, clear the destination StringBuilder first when replacement rather than concatenation is desired.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UndropTagType
$htmlToXml->UndropTagType($tagName);
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.
Returns the object, so calls can be chained.
topUndropTextFormattingTags
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.
Returns the object, so calls can be chained.
Xml
my $str = $htmlToXml->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 undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.