Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parser JSoup change the tags to lower case letter

Tags:

java

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?

like image 986
Wilson Ribeiro Avatar asked Oct 29 '13 18:10

Wilson Ribeiro


4 Answers

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:

  1. If you want a clean solution, you can clone / download the JSoup Git and change this line.
  2. If you want a dirty solution, you can use reflection.

Example for #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
like image 97
ollo Avatar answered Oct 18 '22 02:10

ollo


You must use xmlParser instead of htmlParser and the tags will remain unchanged. One line does the trick:

String html = "<camelCaseTag>some text</camelCaseTag>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
like image 36
Uroš Vonta Avatar answered Oct 18 '22 02:10

Uroš Vonta


Here is a code sample (version >= 1.11.x):

Parser parser = Parser.htmlParser();
parser.settings(new ParseSettings(true, true));
Document doc = parser.parseInput(html, baseUrl);
like image 4
Investigator Avatar answered Oct 18 '22 01:10

Investigator


There is ParseSettings class introduced in version 1.9.3. It comes with options to preserve case for tags and attributes.

like image 3
qza Avatar answered Oct 18 '22 02:10

qza