Parser JSoup将标记更改为小写字母 [英] Parser JSoup change the tags to lower case letter
本文介绍了Parser JSoup将标记更改为小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了一些研究,似乎标准的Jsoup做了这个改变。我想知道是否有办法配置这个或者是否有其他解析器我可以转换为Jsoup的文档,或者某种方法来解决这个问题?
I did some research and it seems that is standard Jsoup make this change. I wonder if there is a way to configure this or is there some other Parser I can be converted to a document of Jsoup, or some way to fix this?
推荐答案
不幸的是, Tag
类的构造函数将名称更改为小写:
Unfortunately not, the constructor of Tag
class changes the name to lower case:
private Tag(String tagName) {
this.tagName = tagName.toLowerCase();
}
但有两种方法可以改变这种行为:
But there are two ways to change this behavour:
- 如果你想要一个 clean 解决方案,你可以克隆/下载 JSoup Git 并更改此行。
- 如果你想要一个脏解决方案,你可以使用反射。
- If you want a clean solution, you can clone / download the JSoup Git and change this line.
- If you want a dirty solution, you can use reflection.
#2示例:
Field tagName = Tag.class.getDeclaredField("tagName"); // Get the field which contains the tagname
tagName.setAccessible(true); // Set accessible to allow changes
for( Element element : doc.select("*") ) // Iterate over all tags
{
Tag tag = element.tag(); // Get the tag of the element
String value = tagName.get(tag).toString(); // Get the value (= name) of the tag
if( !value.startsWith("#") ) // You can ignore all tags starting with a '#'
{
tagName.set(tag, value.toUpperCase()); // Set the tagname to the uppercase
}
}
tagName.setAccessible(false); // Revert to false
这篇关于Parser JSoup将标记更改为小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文