Class HtmlToXml

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.HtmlToXml
All Implemented Interfaces:
AutoCloseable

public class HtmlToXml extends ChilkatObject
<section class="htmltoxml-summary">

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 Details

    • HtmlToXml

      public HtmlToXml()
      Creates a new HtmlToXml object.
  • 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 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.

      Returns:
      the value of the DropCustomTags property
    • setDropCustomTags

      public void setDropCustomTags​(boolean value)
      Sets the DropCustomTags property; see getDropCustomTags().
      Parameters:
      value - the new value
    • getHtml

      public String getHtml()
      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.

      Returns:
      the value of the Html property
    • setHtml

      public void setHtml​(String value)
      Sets the Html property; see getHtml().
      Parameters:
      value - the new value (not null)
    • getNbsp

      public int getNbsp()
      Controls how HTML non-breaking-space entities are handled during conversion. The default value is 0.

      Value Behavior for &nbsp;

      0 Convert to a normal space character (U+0020).

      1 Convert to the numeric character reference &#160; (U+00A0).

      2 Drop the non-breaking-space entity.

      3 Leave the &nbsp; entity unconverted by this option.

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

      Returns:
      the value of the Nbsp property
    • setNbsp

      public void setNbsp​(int value)
      Sets the Nbsp property; see getNbsp().
      Parameters:
      value - the new value
    • getXmlCharset

      public String getXmlCharset()
      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.

      Returns:
      the value of the XmlCharset property
    • setXmlCharset

      public void setXmlCharset​(String value)
      Sets the XmlCharset property; see getXmlCharset().
      Parameters:
      value - the new value (not null)
    • convertFile

      public void convertFile​(String inHtmlPath, String 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 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.

      Parameters:
      inHtmlPath - the inHtmlPath argument (not null)
      destXmlPath - the destXmlPath argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • dropTagType

      public void dropTagType​(String 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.

      Parameters:
      tagName - the tagName argument (not null)
    • 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, 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

      public void setHtmlBd​(BinData bd)
      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.

      Parameters:
      bd - the bd argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setHtmlFromFile

      public void setHtmlFromFile​(String 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.

      Parameters:
      filename - the filename argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setHtmlSb

      public void setHtmlSb​(StringBuilder 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 true on success and false on failure.

      Parameters:
      sb - the sb argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • toXml

      public String 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.

      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

      public void toXmlSb​(StringBuilder sb)
      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.

      Parameters:
      sb - the sb argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • undropTagType

      public void undropTagType​(String 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.

      Parameters:
      tagName - the tagName argument (not null)
    • 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, and sup.

      Call DropTextFormattingTags to return to dropping these formatting tags.

    • xml

      public String 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 result (never null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why