The Java implementation idea of sitemap file generation (Turn)

Source: Internet
Author: User
in the Web site operations, especially in the process of SEO, the role of Sitemap can not be overlooked. Let's look at the protocol and specifications for Sitemap first. Google Sitemap protocol is Google's launch of a site map protocol, which was upgraded on the basis of earlier Robots.txt file protocols. Google Webmaster Tools to point out that "the addition of Google sitemap files will be more conducive to Google web crawler crawling robot indexing, which will improve the efficiency and accuracy of indexing site content." The sitemap file protocol uses a simple XML format, with a total of 6 tags, with key tags including link addresses, update times, update frequencies, and index precedence. Google sitemap files are generated in the following format: <urlset xmlns= "Http://www.google.com/schemas/sitemap/0.84″; > <url> <loc> http://ecvip.org<;/loc > <lastmod>2005-06-03</lastmod> <changefreq The functions of >always</changefreq> <priority>1.0</priority> </url> </urlset> above six labels respectively are: Changefreq: Page content Update frequency: Divided into always, hourly, daily, weekly, monthly, yearly several; Lastmod: Page last modified: Googlebot will determine whether to index again next time; Loc: Page permanent link Address: specific link address; Priority: Priority relative to other pages: between 0.0-1.0; URL: Parent tag relative to the first 4 tags: webmaster want to display each link in the sitemap file with and include; Urlset: Parent tags relative to the first 5 tags: the namespace that defines this XML file is equivalent to the label in the Web page file. As I recently led a group of people in the completion of large-scale E-commerce Web site projects, and the project is completed by the Java EE Architecture, and then consider whether you can let the Code self-service daily generation E-commerce site sitemap files. According to this idea, so began to conceive, in front of me there are three questions: 1, get all links to the site. 2, the generation of XML file 3, the two problems after the regular call if the person who has done the Java program, should be better than the solution. And how to get all the links to the site, as if not previously done this piece, and this idea shouldIt's a little imagined with the current reptile technology, so find the Nutch framework on the web. This framework has actually been heard before, but also more famous. Then found a friend on the network to study his source code, which has an article on the first problem can be clearly resolved. So all code architectures are selected, consisting of httpclient+nekohtml+jdom+spring. Here to intercept the partial get site link code private static void GetText (StringBuffer SB, node node) {if (node.getnodetype () = = Node.text_node) {SB.A Ppend (Node.getnodevalue ())//Get the node value, that is, the information between the start and end tags nodelist children = node.getchildnodes (); if (children!= null) {int len = children.getlength (); for (int i = 0; I <> getText (SB, Children.item (0));//recursively traverse DOM Tree}} private void Getoutlinks (URL base, Map outlinks, node node, int level) {if (node.getnodetype () = = Node.element_no DE) {if ("a". Equalsignorecase (Node.getnodename ())) {StringBuffer linkText = new StringBuffer (); GetText (linkText, node) ; NamedNodeMap attrs = Node.getattributes (); String target = null; for (int i = 0; I <> if ("href". Equalsignorecase (Attrs.item (0). Getnodename ())) {target = Attrs.item (0). Getnodevalue ();//In the DOM tree, the property is a node. Break } if (target!= null) try {URL url = new URL (base, target); if (StrIngutil.contains (Url.tostring (), SiteURL)) {String Urllink=urlparamdecoder (url.tostring ()); if (level = 1) { Outlinks.put (url.tostring), New Outlink (Urllink, New Date (), Daily, 0.9″, linktext.tostring (). Trim (), level); else {outlinks.put (url.tostring), New Outlink (Urllink, New Date (), Daily, 0.8″, linktext.tostring (). Trim (), level) ; The catch (Malformedurlexception e) {logger.error (e);}}} } nodelist Children = Node.getchildnodes (); if (children!= null) {int len = children.getlength (); for (int i = 0; I <> getoutlinks (base, Outlinks, children.it EM (i), level);//recursively Traverse DOM tree}} private Map gethttpnode (String url, int level) {//Initialize MAP SiteMap = new HashMap (); if (Str Ingutil.containsnone (URL, siteurl)) {return siteMap;} HttpClient client = new HttpClient (); Create a file Fragment object DocumentFragment node = new Htmldocumentimpl (). Createdocumentfragment (); url = urlparamencoder (URL); if (proxyhost!= null) {client.gethostconfiguration (). SetProxy (ProxyHost, proxyport);} client. Gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (ConnectionTimeout); Client.gethttpconnectionmanager (). Getparams (). Setsotimeout (SoTimeout); GetMethod method = new GetMethod (URL); Method.addrequestheader ("Content-type", "text/html; Charset=utf-8″); Method.getparams (). Setparameter (Httpmethodparams.retry_handler, New Defaulthttpmethodretryhandler ()); try {int statusCode = Client.executemethod (method); if (StatusCode!= httpstatus.sc_ok) {throw new HttpException ("Httpst Atuscode: "+ StatusCode);} InputStream InputStream = Method.getresponsebodyasstream (); Parser.setproperty ("http://cyberneko.org/html/properties/default-encoding", "Utf-8″"); Parser.setfeature ("Http://cyberneko.org/html/features/scanner/script/strip-comment-delims", true); Parser.parse (New InputSource (InputStream), node); Getoutlinks (new URL (URL), SITEMAP, node, level); catch (Saxexception se) {logger.error (SE);} catch (HttpException him) {logger.error (he);} catch (IOException IE) {lo Gger.error (IE); }catch (Exception e) {logger.error (e);} finally {//release connection method.releaseconnection ();}//Return to the result returned SITEMAP; In the process of writing code, the problem to be solved is that if the website has Chinese parameters, the httpclient will report the URL parameter error, so it is necessary to use Urlencoder.encode plus processing. At the same time, when generating XML files using Jdom, all elements are brought up with namespace parameters because Jdom generates namespace XML. The last is to put the generated XML file in the root directory of the site to create a directory called Sitemapxml. Then submit Sitemap files to several well-known search engines. Google

Submit Site Map Sitemap to Google: Submit via URL management below; http://www.google.com/webmasters/tools/use Google Account login, verify your site, and then you can then "sitemap" page submitted the need to submit your site Sitemap address for http://$url /sitemapxml/sitemap.xml Yahoo China

Submit site Map Sitemap to Yahoo China, submit http://sitemap.cn.yahoo.com/mysites to MSN site Map sitemap via the following URL management, submitted directly with URLs:

http://api.moreover.com/ping?u=http%3A//$url/sitemapxml/sitemap.xml submit site Map Sitemap to ask for direct submission

http://submissions.ask.com/ping?sitemap=http%3A//$url/sitemapxml/sitemap.xml to Baidu to submit site Map Sitemap, no way, Now Baidu does not support Sitemap.

But can be passed through http://www.baidu.com/search/url_submit.html%E6%9D%A5%E6%8F%90%E4%BA%A4%E4%BD%A0%E7%9A%84%E7%BD%91%E5%9D%80. Baidu will search its own, through the robots.txt crawl your sitemap, updated quickly. Add to your website robots.txt file: sitemap:http://$url/sitemapxml/sitemap. xml http://$url/sitemap/index.html (If you don't have a robots.txt file, add the robots.txt file at the root of the site) of course, finally, if you use XSLT to combine XML files, generate HTML-form site maps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.