Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 在jsoup中获取html字符串中的所有属性_Java_Jsoup - Fatal编程技术网

Java 在jsoup中获取html字符串中的所有属性

Java 在jsoup中获取html字符串中的所有属性,java,jsoup,Java,Jsoup,我有HTML格式的字符串,我正在尝试使用Jsoup获取所有属性及其值 字符串是 String string= "<button class=submit btn primary-btn flex-table-btn js-submit type=submit>Sign in</button>"; Document doc = Jsoup.parse(string); try { org.jsoup.nodes.Attributes attrs

我有HTML格式的字符串,我正在尝试使用Jsoup获取所有属性及其值

字符串是

String string= 
"<button class=submit btn primary-btn flex-table-btn js-submit type=submit>Sign in</button>";

 Document doc = Jsoup.parse(string);
    try {
        org.jsoup.nodes.Attributes attrs = doc.attributes();

        for( org.jsoup.nodes.Element element : doc.getAllElements() )
        {
              for( Attribute attribute : element.attributes() )
              {
                  System.out.println( attribute.getKey() +  " --::-- "+attribute.getValue()  ); 
              }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
但我得到的是这个

key:class,值为:submit
键:btn,值为:
键:主btn,值为:
键:柔性表btn,值为:
键:js submit,值为:
键:类型,值为:提交

这是因为引用。如果我使用

String string= 
"<button class='submit btn primary-btn flex-table-btn js-submit' type='submit'>       Sign in</button>";
String=
“登录”;
我将获得所需的输出。但我正在尝试不带引号的输出。

如果没有引号,您将无法执行此操作,因为引号不是可选的。在没有引号的情况下,您引用的HTML描述了一个带有one类(
submit
)的元素和一系列非类、无效的附加属性,这些属性的名称包括
btn
flex table
,等等,任何浏览器都会这样解释它,就像JSoup所做的那样。如果这些是元素上的附加类,则需要引号

发件人:

不带引号的属性值语法

属性名称,后跟零个或多个空格字符,后跟单个U+003D等号字符,后跟零个或多个空格字符,后跟属性值,除上述属性值要求外,不得包含任何文字空格字符,任何U+0022引号字符(“)、U+0027撇号字符(')、“=”(U+003D)字符、”(U+003E)字符或U+0060重重音字符(`),且不得为空字符串


请注意,我强调的“不得包含任何文本空格字符”部分。

使用
Jsoup
很简单:

Document doc = Jsoup.parse(HTML);
List<String> tags = new ArrayList<String>(); //record tags

for(Element e : doc.getAllElements()){      // all elements in html

    tags.add(e.tagName().toLowerCase());    // add each tag in tags List
    //System.out.println("Tag: "+ e.tag()+" attributes = "+e.attributes());  // attributes with values in string
    //System.out.println("Tag: "+ e.tag()+" attributes = "+e.attributes().asList()); //attributes in List<Attribute>

    for(Attribute att : e.attributes().asList()){ // for each tag get all attributes in one List<Attribute>
        System.out.print("Key: "+att.getKey()+ " , Value: "+att.getValue());
        System.out.println();
    }
}

System.out.println("*****************");
System.out.println("All Tags = "+tags);
System.out.println("Distinct Tags = "+ new HashSet<String>(tags));
documentdoc=Jsoup.parse(HTML);
列表标记=新建ArrayList();//记录标记
对于(元素e:doc.getAllegements()){//html中的所有元素
tags.add(e.tagName().toLowerCase());//在标记列表中添加每个标记
//System.out.println(“标记:“+e.Tag()+”attributes=“+e.attributes());//值为字符串的属性
//System.out.println(“标记:“+e.Tag()+”attributes=“+e.attributes().asList());//列表中的属性
对于(属性att:e.attributes().asList()){//对于每个标记,在一个列表中获取所有属性
系统输出打印(“键:+att.getKey()+”,值:+att.getValue());
System.out.println();
}
}
System.out.println(“*******************”);
System.out.println(“所有标签=”+标签);
System.out.println(“Distinct Tags=“+新HashSet(Tags));
Document doc = Jsoup.parse(HTML);
List<String> tags = new ArrayList<String>(); //record tags

for(Element e : doc.getAllElements()){      // all elements in html

    tags.add(e.tagName().toLowerCase());    // add each tag in tags List
    //System.out.println("Tag: "+ e.tag()+" attributes = "+e.attributes());  // attributes with values in string
    //System.out.println("Tag: "+ e.tag()+" attributes = "+e.attributes().asList()); //attributes in List<Attribute>

    for(Attribute att : e.attributes().asList()){ // for each tag get all attributes in one List<Attribute>
        System.out.print("Key: "+att.getKey()+ " , Value: "+att.getValue());
        System.out.println();
    }
}

System.out.println("*****************");
System.out.println("All Tags = "+tags);
System.out.println("Distinct Tags = "+ new HashSet<String>(tags));