HtmlToXml Chilkat2 PHP Reference Documentation
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\HtmlToXml;
try {
$htmlToXml = new HtmlToXml();
// ... use $htmlToXml ...
} catch (\Chilkat2\Exception $e) {
echo $e->getClass(), '::', $e->getMethod(), " failed\n";
echo $e->getMessage(), "\n"; // the LastErrorText of the failed call
}Creates the object. The optional array sets properties in order, as if assigned one by one (new HtmlToXml(['VerboseLogging' => true, 'RaiseError' => false])); an unknown or read-only name throws Error. The class is defined by the chilkat2 extension (extension=chilkat2 in php.ini): there is no PHP file to include.
There is no close(): PHP frees the object when its last reference goes (unset($htmlToXml), $htmlToXml = null, or the end of the function or request). Objects returned by methods are independent and freed the same way. The class may be extended (class MyHtmlToXml extends \Chilkat2\HtmlToXml); objects cannot be cloned or serialized.
When true (the default), a method that fails throws Chilkat2\Exception. When false, it returns false (or null) instead, and the reason is in LastErrorText. Objects returned by a method inherit the value of the object that returned them.
Errors
Chilkat2\Exception extends RuntimeException: getMessage() is the failed call's full LastErrorText, getClass() its class ('Chilkat2\HtmlToXml') and getMethod() the method name. The object remains usable. Methods that look something up and find nothing return null without throwing (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain bool, and property reads and writes do not throw for Chilkat reasons.
Misuse always throws, whatever RaiseError is: a wrong argument type is a TypeError (exact types under declare(strict_types=1)), a missing or extra argument an ArgumentCountError, a string containing a NUL byte or an out-of-range integer a ValueError, and an unknown property (properties are case-sensitive) or a write to a read-only one an Error. Named arguments work with the parameter names shown.
Strings are passed and returned as UTF-8; binary data is a PHP string of bytes. Unlocking: call (new \Chilkat2\GlobalSettings())->UnlockBundle('...') once at startup.
Properties
DebugLogFilePath
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
Controls whether tags that Chilkat does not recognize as standard HTML elements are omitted from the generated XML.
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.Html
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
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
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
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
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.
Nbsp
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
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.
Version
XmlCharset
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
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.
Returns true on success. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
DropTagType
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.
DropTextFormattingTags
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.
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 true on success and false on failure.
Returns true on success. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
SetHtmlFromFile
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. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
SetHtmlSb
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. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
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.On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns null). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
ToXmlSb
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. On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns false). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.
UndropTagType
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.
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.
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.
On failure it throws Chilkat2\Exception (or, with RaiseError set to false, returns null). $e->getMessage() (or the object's LastErrorText) explains why, and the object remains usable.