Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java删除冗余的html标记_Java_Html - Fatal编程技术网

Java删除冗余的html标记

Java删除冗余的html标记,java,html,Java,Html,比如说我有这样的html <b><b>hello</b></b> <b><i>hello</i></b> <b><b><i><b>hello</b></i></b></b> <b><u><b><i><u><i>hello</i&g

比如说我有这样的html

<b><b>hello</b></b>
<b><i>hello</i></b>
<b><b><i><b>hello</b></i></b></b>
<b><u><b><i><u><i>hello</i></u></i></b></u></b>
<b>hello</b>
<b><i>hello</i></b>
<b><i>hello</i></b>
<b><u><i>hello</i></u></b>
你好 你好 你好 你好 如何删除/合并冗余标记,使输出看起来像这样

<b><b>hello</b></b>
<b><i>hello</i></b>
<b><b><i><b>hello</b></i></b></b>
<b><u><b><i><u><i>hello</i></u></i></b></u></b>
<b>hello</b>
<b><i>hello</i></b>
<b><i>hello</i></b>
<b><u><i>hello</i></u></b>
你好 你好 你好 你好
我认为在java中最简单的方法是使用String.replace API截断额外的HTML标记

例如:

public static void main(String[] args) {
    String line = "<b><b>hello</b></b>";
    String[] tagsToRemove = { "b", "i" }; // Extend this to include more tags to remove

    for (String tagToRemove : tagsToRemove) {
        line = delRedundantTags(line, tagToRemove);
    }
    System.out.println(line);
}

private static String delRedundantTags(String html, String tag) {
    String headTagPattern = "<" + tag + ">";
    String endTagPattern = "</" + tag + ">";
    return html.replace(headTagPattern + headTagPattern, headTagPattern). /*Remove the front tag*/
                replace(endTagPattern + endTagPattern, endTagPattern);
}
publicstaticvoidmain(字符串[]args){
String line=“hello”;
String[]tagsToRemove={“b”,“i”};//扩展此项以包含更多要删除的标记
用于(字符串tagToRemove:tagsToRemove){
line=delRedundantTags(line,tagToRemove);
}
系统输出打印项次(行);
}
私有静态字符串delRedundantTags(字符串html、字符串标记){
字符串headTagPattern=“”;
字符串endTagPattern=“”;
返回html.replace(headTagPattern+headTagPattern,headTagPattern)。/*删除前面的标记*/
替换(endTagPattern+endTagPattern,endTagPattern);
}
这种方法的问题在于,您需要知道哪些是冗余标记,例如,上面的代码只适用于粗体和斜体标记,以删除更多冗余标记,然后您必须扩展tagsToRemove阵列


注意:如果冗余标记之间有空格,则API将不起作用。在这种情况下,您必须使用trim()API或查看正则表达式

这应该是替换重复标记的通用方法

     String line = "<b><b>hello</b></b>";
     line = line.replaceAll("<(.*?)><\\1>", "<$1>");
     System.out.println(line);
String line=“你好”;
line=line.replaceAll(“,”);
系统输出打印项次(行);
印刷品

<b>hello</b>
你好 使用您的代码可能看起来像:

public static void main(String[] args) {
    String html = "<b><b>hello</b></b>\r\n" + "<b><i>hello</i></b>\r\n"
            + "<b><b><i><b>hello</b></i></b></b>\r\n"
            + "<b><u><b><i><u><i>hello</i></u></i></b></u></b>";

    Document doc = Jsoup.parseBodyFragment(html);

    System.out.println("before: ");
    System.out.println(doc.body());

    for (Element el : doc.getAllElements()) {
        if (hasSameTypeAncestor(el)) {
            el.unwrap();
        }
    }

    System.out.println("========");
    System.out.println("after:");
    System.out.println(doc.body());

}

private static Set<String> tagsIDontWantToNest = new HashSet<>(Arrays.asList("b","i","u"));

private static boolean hasSameTypeAncestor(Element element) {

    Tag tag = element.tag();
    //handle only these tags: <b> <i> <u>
    if (tagsIDontWantToNest.contains(tag.getName())) {
        for (Element el : element.parents()) {
            if (el.tag().equals(tag)) {
                return true;
            }
        }
    }
    return false;
}
publicstaticvoidmain(字符串[]args){
字符串html=“hello\r\n”+“hello\r\n”
+“你好\r\n”
+“你好”;
Document doc=Jsoup.parseBodyFragment(html);
System.out.println(“之前:”);
System.out.println(doc.body());
对于(元素el:doc.getAllegements()){
if(HassameType(el)){
el.展开();
}
}
System.out.println(“=============”);
System.out.println(“之后:”);
System.out.println(doc.body());
}
私有静态集tagsidontwantonest=newhashset(Arrays.asList(“b”、“i”、“u”));
私有静态布尔HassameType祖先(元素){
Tag Tag=element.Tag();
//仅处理这些标记:
if(tagsidontwantonest.contains(tag.getName())){
对于(元素el:Element.parents()){
如果(el.tag()等于(tag)){
返回true;
}
}
}
返回false;
}
输出:

before: 
<body>
 <b><b>hello</b></b> 
 <b><i>hello</i></b> 
 <b><b><i><b>hello</b></i></b></b> 
 <b><u><b><i><u><i>hello</i></u></i></b></u></b>
</body>
========
after:
<body>
 <b>hello</b> 
 <b><i>hello</i></b> 
 <b><i>hello</i></b> 
 <b><u><i>hello</i></u></b>
</body>
之前:
你好
你好
你好
你好
========
之后:
你好
你好
你好
你好

这不适用于hello和hellowoops抱歉,我没有注意到其他要求。对于这种情况,@Pshemo的解决方案无疑是可行的。