gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:24:53 26 4
gpt4 key购买 nike

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

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

.....

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

priorityElementNode.setTextContent(getPriority(location));

....

关于java - 在java中将xml解析为字符串以在sitemap.xml中给出优先级值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58402739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com