Class HtmlToXml
- All Implemented Interfaces:
AutoCloseable
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.
For an extended overview, see HtmlToXml Class Overview.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidconvertFile(String inHtmlPath, String destXmlPath)Converts the HTML file atinHtmlPathto a well-formed XML file atdestXmlPath.voiddropTagType(String tagName)Marks the specified HTML tag type to be dropped from the generated XML.voidConfigures the converter to drop common text-formatting tags from the generated XML.booleanControls whether tags that Chilkat does not recognize as standard HTML elements are omitted from the generated XML.getHtml()The HTML source to be converted.intgetNbsp()Controls how HTML non-breaking-space entities are handled during conversion.Specifies the character encoding to identify for the generated XML, such asutf-8oriso-8859-1.voidsetDropCustomTags(boolean value)Sets theDropCustomTagsproperty; seegetDropCustomTags().voidSets theHtmlproperty; seegetHtml().voidReplaces the current HTML source with the complete contents ofbd.voidsetHtmlFromFile(String filename)Loads HTML fromfilenameand replaces the currentHtmlinput with the file contents.voidsetHtmlSb(StringBuilder sb)Replaces the current HTML source with the complete contents ofsb.voidsetNbsp(int value)Sets theNbspproperty; seegetNbsp().voidsetXmlCharset(String value)Sets theXmlCharsetproperty; seegetXmlCharset().toXml()Converts the currentHtmlinput to well-formed XML and returns the XML string.voidtoXmlSb(StringBuilder sb)Converts the currentHtmlinput to well-formed XML and appends the generated XML tosb.voidundropTagType(String tagName)Reverses a previousDropTagTypesetting for the specified tag name so that tag type is no longer intentionally dropped from subsequent XML output.voidConfigures the converter to retain the common text-formatting tags that are otherwise dropped by default:xml()Equivalent toToXml.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
HtmlToXml
public HtmlToXml()Creates a newHtmlToXmlobject.
-
-
Method Details
-
getDropCustomTags
public boolean getDropCustomTags()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
truewhen custom or application-specific tags would add noise to the XML structure. Leave itfalsewhen those tags are meaningful to the application and should remain represented in the converted output.- Returns:
- the value of the
DropCustomTagsproperty
-
setDropCustomTags
public void setDropCustomTags(boolean value)Sets theDropCustomTagsproperty; seegetDropCustomTags().- Parameters:
value- the new value
-
getHtml
The HTML source to be converted. Set this property to an HTML string, configure any conversion options, and then callToXml,Xml, orToXmlSb.Assigning this property replaces the current in-memory HTML source. For file-to-file conversion, use
ConvertFile. To load HTML from a file,BinData, orStringBuilder, use the correspondingSetHtml*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.- Returns:
- the value of the
Htmlproperty
-
setHtml
Sets theHtmlproperty; seegetHtml().- Parameters:
value- the new value (notnull)
-
getNbsp
public int getNbsp()Controls how HTML non-breaking-space entities are handled during conversion. The default value is0.Value Behavior for
0Convert to a normal space character (U+0020).1Convert to the numeric character reference (U+00A0).2Drop the non-breaking-space entity.3Leave the entity unconverted by this option.This setting applies specifically to the HTML entity
; it does not define general whitespace normalization.- Returns:
- the value of the
Nbspproperty
-
setNbsp
public void setNbsp(int value)Sets theNbspproperty; seegetNbsp().- Parameters:
value- the new value
-
getXmlCharset
Specifies the character encoding to identify for the generated XML, such asutf-8oriso-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
ToXmlorXmlis a string in the programming language's native string representation, while the XML declaration reflectsXmlCharset. When writing that string to a file, use the same charset so the file bytes agree with the XML declaration.ConvertFileperforms the file conversion directly.- Returns:
- the value of the
XmlCharsetproperty
-
setXmlCharset
Sets theXmlCharsetproperty; seegetXmlCharset().- Parameters:
value- the new value (notnull)
-
convertFile
Converts the HTML file atinHtmlPathto a well-formed XML file atdestXmlPath. The output encoding is controlled byXmlCharset; ifXmlCharsetis empty, Chilkat uses the HTML source encoding.Returns
trueon success andfalseon 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.
- Parameters:
inHtmlPath- theinHtmlPathargument (notnull)destXmlPath- thedestXmlPathargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
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
HtmlToXmlobject. UseUndropTagTypeto reverse the setting for a particular tag type.- Parameters:
tagName- thetagNameargument (notnull)
-
dropTextFormattingTags
public void 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, andsup.This is useful when the formatting markup is not relevant to later XML parsing and the application wants a simpler structural representation. Call
UndropTextFormattingTagsto keep these tags instead. -
setHtmlBd
Replaces the current HTML source with the complete contents ofbd.The data is copied from the supplied
BinDataobject and becomes the input for a later call toToXml,Xml, orToXmlSb. Returnstrueon success andfalseon failure.- Parameters:
bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setHtmlFromFile
Loads HTML fromfilenameand replaces the currentHtmlinput with the file contents.Returns
trueon success andfalseif the file cannot be read or the HTML input cannot be loaded. CallToXml,Xml, orToXmlSbafterward to perform the conversion.- Parameters:
filename- thefilenameargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setHtmlSb
Replaces the current HTML source with the complete contents ofsb.The text is copied from the supplied
StringBuilderand becomes the input for a later call toToXml,Xml, orToXmlSb. Returnstrueon success andfalseon failure.- Parameters:
sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
toXml
Converts the currentHtmlinput to well-formed XML and returns the XML string. The generated XML uses the conversion settings currently configured on this object, includingDropCustomTags, dropped tag types,Nbsp, andXmlCharset.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 result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
toXmlSb
Converts the currentHtmlinput to well-formed XML and appends the generated XML tosb.Returns
trueon success andfalseon failure. Because the result is appended, clear the destinationStringBuilderfirst when replacement rather than concatenation is desired.- Parameters:
sb- thesbargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
undropTagType
Reverses a previousDropTagTypesetting 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.
- Parameters:
tagName- thetagNameargument (notnull)
-
undropTextFormattingTags
public void undropTextFormattingTags()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, andsup.Call
DropTextFormattingTagsto return to dropping these formatting tags. -
xml
Equivalent toToXml. Converts the currentHtmlinput 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 result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-