简体   繁体   中英

how get Only text from Javafx2 HTMLEditor

I have a HTMLEditor and i have typed "My Simple Text".

 @FXML
 public HTMLEditor htmlEditor;

when say

htmlEditor.getHtmlText();

this return

<html><head></head><body contenteditable="true"><p style="text-align: left;"><font face="'Segoe UI'">My Simple Text</font></p></body></html>

But want text without html tag ie

My Simple Text

how can i do it?

This is actually dead simple with Jsoup .

public static String html2text(String html) {
    return Jsoup.parse(html).text();
}

source : Remove HTML tags from a String

After applying getHtmlText() on your HtmlEditor pass the resulted String of html code to the following method:

public static String getText(String htmlText) {

  String result = "";

  Pattern pattern = Pattern.compile("<[^>]*>");
  Matcher matcher = pattern.matcher(htmlText);
  final StringBuffer text = new StringBuffer(htmlText.length());

  while (matcher.find()) {
    matcher.appendReplacement(
      text,
      " ");
  }

  matcher.appendTail(text);

  result = text.toString().trim();  

  return result;
}

You should remove all html tags from your Html Text you get from this HTMLEditor.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;


public class HTMLEditorDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        HTMLEditor editor = new HTMLEditor();
        Button b = new Button("Show Text");
        b.setOnAction((ActionEvent e) -> {
            String htmlText = editor.getHtmlText();
            stripHTMLTags(htmlText);

        });

        VBox vBox = new VBox(b, editor);
        Scene scene = new Scene(vBox, 800, 600);


        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private void stripHTMLTags(String htmlText) {

        Pattern pattern = Pattern.compile("<[^>]*>");
        Matcher matcher = pattern.matcher(htmlText);
        final StringBuffer sb = new StringBuffer(htmlText.length());
        while(matcher.find()) {
            matcher.appendReplacement(sb, " ");
        }
        matcher.appendTail(sb);
        System.out.println(sb.toString().trim());

    }

}

Patrick

Try this

 WebView webView = (WebView) htmlEditor.lookup("WebView");
    ((HTMLBodyElementImpl) ((NodeListImpl) webView.getEngine().getDocument().getElementsByTagName("body")).item(0)).getTextContent();

you can also set to content text or focus on webview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM