在 java 中将 xml 解析为字符串,以在站点地图.xml中给出优先级值



在我的网站根目录中,java servlet准备好了content/falcon/en/index之后的所有文件并创建一个站点地图.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中编写站点地图时有这种方法.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));
....

最新更新