java - 在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>
我在java中有这个方法写sitemap.xml
public void createXMLNode(Document document, Element rootElement, SlingHttpServletRequest request, Iterator<Page> pageIterator) {
Element headElement = document.createElement("url");
Element locElement = document.createElement("loc");
Element lastModElement = document.createElement("lastMod");
Element changefreqElement = document.createElement("changefreq");
Element priorityElement = document.createElement("priority");
Node locElementNode = locElement;
Node lastModElementNode = lastModElement;
Node changefreqElementNode = changefreqElement;
Node priorityElementNode = priorityElement;
Page childPage = pageIterator.next();
locElementNode.setTextContent(request.getScheme() + "://" + request.getServerName() + childPage.getPath());
LOG.error("childPage.getLastModified()" + childPage.getLastModified());
if(null != childPage.getLastModified()) {
Date date = childPage.getLastModified().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
dateFormat.parse("2019-07-15");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lastModElementNode.setTextContent(dateFormat.format(date));
}
changefreqElementNode.setTextContent("weekly");
priorityElementNode.setTextContent("0.9");
rootElement.appendChild(headElement);
headElement.appendChild(locElementNode);
headElement.appendChild(lastModElementNode);
headElement.appendChild(changefreqElementNode);
headElement.appendChild(priorityElementNode);
Iterator<Page> childPageIterator = childPage.listChildren();
while(childPageIterator.hasNext()) {
createXMLNode(document, rootElement, request, childPageIterator);
}
}
正如你所看到的,0.9 是硬编码的优先级我在 java 中编写了这个程序,它通过字符串中反斜杠的数量给出优先级值
public class StringArray {
public static void main (String[]args){
int count = 0;
double priority=0;
String [] array = {"https://www.farmers.com/", "https://www.farmers.com/css/login/","https://www.farmers.com/auto/" ,"https://www.farmers.com/customerselfservice/CSSU" };
for(int i =0;i<array.length;i++){
for(int z = 0;z<array[i].length();z++){
if(array[i].charAt(z) == '/')
{
count++;
}
}
System.out.println(count);
if (count == 3){
priority=1;
}
else if(count==4)
{
priority = 0.9;
}
else if(count==5)
{
priority = 0.8;
}
else if(count==6)
{
priority =0.7;
}
System.out.println("<priority>" + priority +"</priority>" );
count =0;
}
}
}
我如何将我的优先级值算法与这个 java 方法集成。我必须将 loc 值解析为字符串,因此我可以进行比较
解决方案
您可以添加以下方法来设置优先级:
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));
....
推荐阅读
- python - Python 3.7 else 语句没有显示正确的索引?
- reactjs - 用 Jest Mock 函数测试 onClick
- laravel - Laravel 使用外键获取旧数据库中的数据并将其复制到新数据库
- python - 为什么 Django QuerySet 返回的值比数据库中的对象数(重复对象)多?
- api - Telegram API - 如何从邀请链接获取数据?聊天 ID、图像、标题、描述
- reactjs - 如何正确导入 Web 组件以响应应用程序?
- python-3.x - 同步pymol
- reactjs - 在 netlify 上托管时未捕获(承诺)
- wpf - 如何在打开事件中获取上下文菜单对象
- python - 每个带有文件名的 pdf 添加 2 页