Java从元素中获取包含换行符的文本内容

Java从元素中获取包含换行符的文本内容,java,html,javafx,javafx-webengine,Java,Html,Javafx,Javafx Webengine,我有一个元素,它是从WebEngine(JavaFX)的文档中获取的,我使用body元素的getTextContent()函数来获取文本内容。body元素具有属性contenteditable=“true”,因此我可以在其上书写。但是,从getTextContent()返回的字符串不包括换行符。所以 Line 1 Line 2 Line 3 在主体上,将返回第1行第2行第3行我需要它包含换行符。我怎样才能让它这样做 或者,我可以使用而不是后续行将作为元素的子元素插入html中 您可以通过执行一

我有一个
元素
,它是从
WebEngine
(JavaFX)的
文档
中获取的,我使用body元素的
getTextContent()
函数来获取文本内容。body元素具有属性
contenteditable=“true”
,因此我可以在其上书写。但是,从
getTextContent()
返回的字符串不包括换行符。所以

Line 1
Line 2
Line 3
在主体上,将返回
第1行第2行第3行
我需要它包含换行符。我怎样才能让它这样做


或者,我可以使用
而不是
后续行将作为
元素的子元素插入html中

您可以通过执行一段javascript来查看HTML内容:

String html = (String)webView.getEngine()
    .executeScript("document.documentElement.innerHTML");
要获得单独的行,需要遍历
的子节点。下面是一个例子:

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class EditableWebView extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        webView.getEngine().loadContent(
            "<html><body contentEditable=\"true\"></body></html>");

        Button contentButton = new Button("Show content");
        contentButton.setOnAction(e -> {
            List<String> lines = new ArrayList<>();

            Node body = webView.getEngine()
                    .getDocument()
                    .getElementsByTagName("body")
                    .item(0);
            NodeList childNodes = body.getChildNodes();
            for (int i=0; i<childNodes.getLength(); i++) {
                lines.add(childNodes.item(i).getTextContent());
            }

            lines.forEach(System.out::println);

        });

        Button htmlButton = new Button("Show HTML");
        htmlButton.setOnAction(e -> 
            System.out.println(webView.getEngine()
                    .executeScript("document.documentElement.innerHTML")));

        HBox buttons = new HBox(5, contentButton, htmlButton);

        BorderPane root = new BorderPane(webView, null, null, buttons, null);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
import java.util.ArrayList;
导入java.util.List;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.web.WebView;
导入javafx.stage.stage;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类EditableWebView扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
WebView WebView=新建WebView();
webView.getEngine().loadContent(
"");
按钮内容按钮=新按钮(“显示内容”);
contentButton.setOnAction(e->{
列表行=新的ArrayList();
节点主体=webView.getEngine()
.getDocument()
.getElementsByTagName(“主体”)
.项目(0);
NodeList childNodes=body.getChildNodes();
对于(int i=0;i
System.out.println(webView.getEngine()
.executeScript(“document.documentElement.innerHTML”);
HBox按钮=新的HBox(5,contentButton,htmlButton);
BorderPane root=新的BorderPane(webView,null,null,按钮,null);
场景=新场景(root,600400);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

如果您想要创建可设置样式的可编辑文本的其他选项,请查看

不完全确定语法,但换行符通常使用“\n”完成。我的意思是,当用户使用contentedite在html正文上键入多行时,使用函数
getTextContent()
忽略类似换行符。返回的字符串中没有任何换行符。谢谢!这正是我需要的答案。