One html formatted string

HTML to formatted text

Are there any java APIs which does similar action like Html.fromHtml() as in Android? JSoup does parse and remove the tags but the output is not a formatted one. eg:

1 Answer 1

There’s no api for jsoup-to-«formated text», but you can convert lists by your own:

  1. iterate over all childs of the ul / ol element which is the root of the list
  2. if item: format and add the output String
  3. if sublist: do 1. — but with the sublist element — and add the result

Example:

In this example i use the type attribute to determine what kind of bullet is required and use the character (!) to index the items. If there’s no proper attribute, char 1 is used.

Implementation:

/** * Convert the Listelement root to a formated string-representation. * * @param root Rootelement of the list (normally 'ul' or 'ol' tag) * @param depth Depth of the list (=0 for root element) * @return List as String */ public String createList(Element root, int depth) < final String indentation = createIndentation(depth); // create indentation StringBuilder sb = new StringBuilder(); final String typeAttr = root.attr("type"); // Get the character used as bullet (= 'type' attribute) char type = typeAttr.isEmpty() ? '1' : typeAttr.charAt(0); // if 'type' attribute: use it, else: use '1' instead for( Element sub : root.children() ) // Iterate over all Childs < // If Java < 7: use if/else if/else here switch( sub.tagName() ) // Check if the element is an item or a sublist < case "li": // Listitem, format and append sb.append(indentation).append(type++).append(". ").append(sub.ownText()).append("\n"); break; case "ol": // Sublist case "ul": if( !sub.children().isEmpty() ) // If sublist is not empty (contains furhter items) < sb.append(createList(sub, depth + 1)); // Recursive call for the sublist >break; default: // "Illegal" tag, do furhter processing if required - output as an example here System.err.println("Not implemented tag: " + sub.tagName()); > > return sb.toString(); // Return the formated List > /** * Create an Indentationstring of length blanks. * * @param length Size of indentation * @return Indentationstring */ private String createIndentation(int length) < StringBuilder sb = new StringBuilder(length); for( int i=0; ireturn sb.toString(); > 

Testcode:

 Document doc = . // Load / parse your document here Element listRoot = doc.select("ol").first(); // Select the root-element (!) of the list here. final String output = createList(listRoot, 0); // Convert the list System.out.println(output); // Ouput 

Result:

1. Test1 a. TestA1 b. TestB1 2. Test2 a. TestA2 b. TestB2 

Источник

Читайте также:  Top forms with css

HTML Text Formatting

HTML contains several elements for defining text with a special meaning.

Example

This is subscript and superscript

HTML Formatting Elements

Formatting elements were designed to display special types of text:

  • — Bold text
  • — Important text
  • — Italic text
  • — Emphasized text
  • — Marked text
  • — Smaller text
  • — Deleted text
  • — Inserted text
  • — Subscript text
  • — Superscript text

HTML and Elements

The HTML element defines bold text, without any extra importance.

Example

The HTML element defines text with strong importance. The content inside is typically displayed in bold.

Example

HTML and Elements

The HTML element defines a part of text in an alternate voice or mood. The content inside is typically displayed in italic.

Tip: The tag is often used to indicate a technical term, a phrase from another language, a thought, a ship name, etc.

Example

The HTML element defines emphasized text. The content inside is typically displayed in italic.

Tip: A screen reader will pronounce the words in with an emphasis, using verbal stress.

Example

HTML Element

The HTML element defines smaller text:

Example

HTML Element

The HTML element defines text that should be marked or highlighted:

Example

Do not forget to buy milk today.

HTML Element

The HTML element defines text that has been deleted from a document. Browsers will usually strike a line through deleted text:

Example

My favorite color is blue red.

HTML Element

The HTML element defines a text that has been inserted into a document. Browsers will usually underline inserted text:

Example

HTML Element

The HTML element defines subscript text. Subscript text appears half a character below the normal line, and is sometimes rendered in a smaller font. Subscript text can be used for chemical formulas, like H2O:

Example

HTML Element

The HTML element defines superscript text. Superscript text appears half a character above the normal line, and is sometimes rendered in a smaller font. Superscript text can be used for footnotes, like WWW [1] :

Example

This is superscripted text.

HTML Exercises

HTML Text Formatting Elements

Tag Description
Defines bold text
Defines emphasized text
Defines a part of text in an alternate voice or mood
Defines smaller text
Defines important text
Defines subscripted text
Defines superscripted text
Defines inserted text
Defines deleted text
Defines marked/highlighted text

For a complete list of all available HTML tags, visit our HTML Tag Reference.

Источник

How can I create a formatted string out of a block of html?

I need to provide a variable bit of data (the «body» of an email) to a (string) member of a list of string, and am trying to build it using string.format, but I get, «) expected» on the «http» part here:

string htmlBodyAmalgamation = string.Format(@"\"Platypus 

", body);

I get that err msg whether I have 0, 1, 2, or 3 backwhacks («\») prepended to the «http». If there is no variable portion (if the body is static/known in advance) I can do this:

List htmlBody = new List < "\"Platypus

Your Platypus Price Push report is attached.

", "" >; mailItem.HTMLBody = string.Join(Environment.NewLine, htmlBody.ToArray());

. and it works fine. So it’s trying to embed the variable «body» value via string.format as follows that is proving problematic:

string htmlBodyAmalgamation = string.Format(@"\"Platypus

Your Platypus Price Push report is attached.

", body); List htmlBody = new List < htmlBodyAmalgamation, "" >;

Источник

Html display formatted text

The text is “pre-formatted” and contains a lot of whit spaces and dashes and every line has the same length (all chars have the same width). Is there a way to display the text in html without losing the format?

give a white-space: pre for the parent div. It should preserve the whitespaces. Do you know if it will contain tags ‘<' or '>‘ or a backslash ‘/’ ?

3 Answers 3

Wrap your text inside the tag.

 
+-001 This is a Line 00:12:04 002 ---------------------------------- - 003 Everthing looks good so far ------

The HTML way is to use the pre element, which has been designed for such usage, but beware that

  • To be on the safe side in formatting, put the
    tag right at the start of the first line and the

    tag right at the end of the last line. Otherwise some browsers may behave as if there were an empty line at the start or at the end of the element.

  • You still need to escape occurrences of the characters < and & in the content (there are some cases where this is not needed, but it is simplest to ignore that.

Example (where I have added a line containing the expression 1 + 1 < 3):

+-001 This is a Line 00:12:04 002 ---------------------------------- - 003 Everthing looks good so far ------ - 004 Simple as 1 + 1 < 3 ------

+-001 This is a Line 00:12:04 002 ---------------------------------- - 003 Everthing looks good so far ------ - 004 Simple as 1 + 1 < 3 ------

Источник

Оцените статью