简体   繁体   English

将html标记格式化为字符串java

[英]Format html tag to a string java

I need help I have a string like this 我需要帮助我有这样的字符串

String myString = "The accuracy of a potentiometer (pH meter) is
                   &#177;0.1 mV. A solution contains 1x10<SUP>-4<\/SUP> mol\/L Cl
                   <SUP>-<\/SUP> and 1x10<SUP>-3<\/SUP> mol\/L Ca<SUP>2+<\/SUP>. 
                   What is the error in concentration to be expected when measuring
                   these ions with a chloride or a calcium ion selective electrode? 
                   <TABLE BORDER=0 ALIGN=CENTER> <TR><TD>    <\/TD><TD>&nbsp;<\/TD><TD>
                   <\/TD><\/TR><\/TABLE>";

I have to show this string in a listView but obviously when I print it there are these html tags. 我必须在listView中显示这个字符串,但显然当我打印它时,这些html标签。

I already tried to get rid of the tags but if so I lost informations (like -4 as superscipt). 我已经试图摆脱标签,但如果是这样我丢失了信息(如-4作为superscipt)。 I'd like to know if it is possible to format the string in a proper way. 我想知道是否可以以正确的方式格式化字符串。

Thanks 谢谢

UPDATE: 更新:

I have partially solved by using Html.fromHtml(String) 我通过使用Html.fromHtml(String)部分解决了

public String processString(String html) {
    Spanned spanned;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        spanned = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
    } else {
        spanned = Html.fromHtml(html);
    }
    return spanned.toString();
}

Now the problem is that the tag is displayed like an obj icon 现在的问题是标签显示为obj图标 图片 and i don't know how to visualize them. 我不知道如何想象它们。

Is it possible? 可能吗?

If you heal the html, it will be rendered fine by many Swing components, without assistance: 如果您修复了html,许多Swing组件将会很好地呈现它,而无需帮助:

import javax.swing.*;
import java.awt.*;

public class Se0124 extends JFrame
{
    String myString = "<html>" +
                   "The accuracy of a potentiometer (pH meter) is" +
                   "&#177;0.1 mV. A solution contains 1x10<SUP>-4</SUP> mol/L Cl" +
                   "<SUP>-</SUP> and 1x10<SUP>-3</SUP> mol/L Ca<SUP>2+</SUP>." +
                   "What is the error in concentration to be expected when measuring" +
                   "these ions with a chloride or a calcium ion selective electrode?" +
                   "</html>";

    public Se0124 ()
    {
        super ("Se0124");
        JPanel mainpanel = new JPanel ();
        mainpanel.setLayout (new BorderLayout ());
        this.getContentPane ().add (mainpanel);

        JLabel jl = new JLabel (myString);
        mainpanel.add (jl, BorderLayout.CENTER);
        setSize (400, 400);
        setLocation (100, 100);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible (true);
    }

    public static void main (String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Se0124 ();
            }
        });
    }
}

As you can see, I have removed the trailing , which looks empty. 如你所见,我已经删除了尾部,它看起来是空的。 Then I capsulated it in tags. 然后我将其封装在标签中。 And I removed the backslashes from the slashes. 我从斜杠中删除了反斜杠。

This can be done by regular expressions. 这可以通过正则表达式来完成。 Now SUP and SUB are rendered fine. 现在SUP和SUB都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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