简体   繁体   English

使用jsoup设置元标记的内容值

[英]Use jsoup to set the content value of a meta tag

I have an HTML document with a number of meta tags 我有一个带有许多元标记的HTML文档

<meta name="donald" content="duck" />
<meta name="micky" content="mouse" />
<meta name="daisy" content="duckling" />
<meta name="scrooge" content="macduck" />

I am able to get the content values ok but wish to change "mouse" to "horse" say. 我能够确定内容值,但希望将“鼠标”改为“马”。

Example: 例:

final String html = "<meta name=\"donald\" content=\"duck\" />\n"
        + "<meta name=\"micky\" content=\"mouse\" />\n"
        + "<meta name=\"daisy\" content=\"duckling\" />\n"
        + "<meta name=\"scrooge\" content=\"macduck\" />";

Document doc = Jsoup.parse(html, "");

Element mouse = doc.select("meta[content=mouse]").first();
mouse.attr("content", "horse");

You just have to select the proper tag and change the attributes value. 您只需要选择适当的标签并更改属性值即可。

Explanation: 说明:

Element mouse = doc.select("meta[content=mouse]").first();

Select all meta -tags, with attribute content with value mouse ; 选择所有带有属性content meta标记,并使用value mouse since select() returns a list of tags, first() is used to get the first one. 由于select()返回标签列表,因此first()用于获取第一个标签。

mouse.attr("content", "horse");

Change value of attribute content . 更改属性content值。

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

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