Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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,我想从锚点标记中删除特定属性: <a id="nav-askquestion" style="cursor:default" href="/questions"> 输出:- <a href="/questions"> 通过java程序人们可能会建议使用正则表达式,但是,您可以使用XML解析器。人们可能会建议使用正则表达式,但是,您可以使用XML解析器。我们用于这类工作 您可以使用此未经测试的snipplet解析和修改节点: NodeVisitor vis

我想从锚点标记中删除特定属性:

<a  id="nav-askquestion" style="cursor:default" href="/questions">

输出:-

<a   href="/questions">


通过java程序

人们可能会建议使用正则表达式,但是,您可以使用XML解析器。

人们可能会建议使用正则表达式,但是,您可以使用XML解析器。

我们用于这类工作

您可以使用此未经测试的snipplet解析和修改节点:

NodeVisitor visitor = new NodeVisitor() {
    public void visitTag(Tag tag) {
            tag.removeAttribute("id");
            tag.removeAttribute("style");
    }

};

Parser parser = new Parser(...);
parser.visitAllNodesWith(visitor);
我们用它来做这种工作

您可以使用此未经测试的snipplet解析和修改节点:

NodeVisitor visitor = new NodeVisitor() {
    public void visitTag(Tag tag) {
            tag.removeAttribute("id");
            tag.removeAttribute("style");
    }

};

Parser parser = new Parser(...);
parser.visitAllNodesWith(visitor);

这个小片段就可以了

问我你是否需要一些关于正则表达式的问题


public class test {

    public static void main(String[] args) {
        String htmlFragment ="<a  id=\"nav-askquestion\" style=\"cursor:default\" href=\"/questions\">";
        String attributesToRemove = "id|style";

        System.out.println(htmlFragment);
        System.out.println(cleanHtmlFragment(htmlFragment, attributesToRemove));
    }

    private static String cleanHtmlFragment(String htmlFragment, String attributesToRemove) {
        return htmlFragment.replaceAll("\\s+(?:" + attributesToRemove + ")\\s*=\\s*\"[^\"]*\"","");    
    }
}

公开课考试{
公共静态void main(字符串[]args){
字符串htmlFragment=“”;
字符串attributesToRemove=“id | style”;
System.out.println(htmlFragment);
System.out.println(cleanHtmlFragment(htmlFragment,attributesToRemove));
}
私有静态字符串cleanHtmlFragment(字符串htmlFragment,字符串属性删除){
返回htmlFragment.replaceAll(“\\s+(?:“+attributesToRemove+”)\\s*=\\s*\“[^\“]*\”“,”);
}
}

这个小片段就可以了

问我你是否需要一些关于正则表达式的问题


public class test {

    public static void main(String[] args) {
        String htmlFragment ="<a  id=\"nav-askquestion\" style=\"cursor:default\" href=\"/questions\">";
        String attributesToRemove = "id|style";

        System.out.println(htmlFragment);
        System.out.println(cleanHtmlFragment(htmlFragment, attributesToRemove));
    }

    private static String cleanHtmlFragment(String htmlFragment, String attributesToRemove) {
        return htmlFragment.replaceAll("\\s+(?:" + attributesToRemove + ")\\s*=\\s*\"[^\"]*\"","");    
    }
}

公开课考试{
公共静态void main(字符串[]args){
字符串htmlFragment=“”;
字符串attributesToRemove=“id | style”;
System.out.println(htmlFragment);
System.out.println(cleanHtmlFragment(htmlFragment,attributesToRemove));
}
私有静态字符串cleanHtmlFragment(字符串htmlFragment,字符串属性删除){
返回htmlFragment.replaceAll(“\\s+(?:“+attributesToRemove+”)\\s*=\\s*\“[^\“]*\”“,”);
}
}

我认为OP想要删除除
href
之外的所有属性。我认为OP想要删除除
href
之外的所有属性。虽然我同意正则表达式会带来它们自己的问题,但不,您不能使用XML解析器。HTML!=XML。即使OP给出的例子也证明了这一点。虽然我同意正则表达式带来了它们自己的问题,但不,您不能使用XML解析器。HTML!=XML。就连OP给出的例子也证明了这一点。