为了账号安全,请及时绑定邮箱和手机立即绑定

在java中将xml解析为字符串以在sitemap.xml中给出优先级值

在java中将xml解析为字符串以在sitemap.xml中给出优先级值

肥皂起泡泡 2023-11-01 16:33:57
在我的网站根目录中,java servlet 已准备好 content/falcon/en/index 后的所有文件并创建 sitemap.xml  <url><loc>https://www.ded.com/</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.ded.com/eFNOL/eFNOL_Login?SO=01</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.ded.com/customerselfservice/CSSU</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.dede.com/claims/roadside-assistance/</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.dede.com/payments/</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.ded.com/insurance/</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url><url><loc>https://www.dede.com/home/</loc><lastmod>2019-08-29</lastmod><changefreq>weekly</changefreq><priority>0.9</priority></url>
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您可以添加以下方法来设置优先级:


public static String getPriority (String location){

    switch(countSlashes(location)){

        case 3: return "1";

        case 4: return "0.9";

        case 5: return "0.8";

        case 6: return "0.7";

        default: return "0.0"; //or whatever prio in default case

    }

}


//replace everything except '/' to get count of slashes easily

private static int countSlashes(String location) {

    return location.replaceAll("[^/]", "").length();

}

然后您可以getPriority从您的createXMLNode方法中调用,如下所示:


.....


String location = request.getScheme() + "://" + request.getServerName() + childPage.getPath();

locElementNode.setTextContent(location);

....


priorityElementNode.setTextContent(getPriority(location));


....


查看完整回答
反对 回复 2023-11-01
  • 1 回答
  • 0 关注
  • 56 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信