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

在我的网站根目录中,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>


肥皂起泡泡
浏览 59回答 1
1回答

郎朗坤

您可以添加以下方法来设置优先级:public static String getPriority (String location){&nbsp; &nbsp; switch(countSlashes(location)){&nbsp; &nbsp; &nbsp; &nbsp; case 3: return "1";&nbsp; &nbsp; &nbsp; &nbsp; case 4: return "0.9";&nbsp; &nbsp; &nbsp; &nbsp; case 5: return "0.8";&nbsp; &nbsp; &nbsp; &nbsp; case 6: return "0.7";&nbsp; &nbsp; &nbsp; &nbsp; default: return "0.0"; //or whatever prio in default case&nbsp; &nbsp; }}//replace everything except '/' to get count of slashes easilyprivate static int countSlashes(String location) {&nbsp; &nbsp; return location.replaceAll("[^/]", "").length();}然后您可以getPriority从您的createXMLNode方法中调用,如下所示:.....String location = request.getScheme() + "://" + request.getServerName() + childPage.getPath();locElementNode.setTextContent(location);....priorityElementNode.setTextContent(getPriority(location));....
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java