Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create an XML sitemap?

 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created an XML sitemap by parsing data from a form then generating all of the relevant nodes and placing that into a large String variable then sending this attribute to the JSP to render.

The issue I have got is that it isn't formatting like an XML document should do within the browser, instead it is simply displaying as normal text and I am a little unsure why.

I have set the ContentType as "text/xml" and I have also tried "application/xml" although none of these work. Using the following code....... response.setContentType("text/xml;charset=UTF-8");

I have also began trying to build a document tree although from my initial tests this was exhibiting the exact same behavior.

Any ideas? I'm sure I must be missing something blindingly obvious but I can't figure it out (yet)


Thanks
Michael
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
White space -like the tabs and newlines that make up such formatting- is not relevant as part of an XML document, unless you're using mixed content (which XML sitemaps don't). Is this an actual problem, or just an aesthetical one?
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I originally added new lines, tabs etc. for making it structured correctly whilst I was testing outputting the String variable into a <textarea> element when outputting to the JSP - although I have since tried removing this and this has made no difference. What I am looking for is for the browser to render the tags into correctly formatted XML Sitemap visually like it does when I check on other websites.

I have a feeling that it is something to do with the response type being set incorrectly since when I access the page which has generated the XML sitemap then the actual ContentType response (when checking via a browser tool - LiveHTTP Headers for FireFox) is still set to "text/html" which isn't what is specified within my Servlet using the code in the previous post.


Thanks
Michael
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Cropper wrote:What I am looking for is for the browser to render the tags into correctly formatted XML Sitemap visually like it does when I check on other websites.


I don't think I've ever seen such a thing, although I've seen a lot of pages which claim to be "sitemaps". Could you post a link to an example on one of those other websites you referred to?

I have a feeling that it is something to do with the response type being set incorrectly since when I access the page which has generated the XML sitemap then the actual ContentType response (when checking via a browser tool - LiveHTTP Headers for FireFox) is still set to "text/html" which isn't what is specified within my Servlet using the code in the previous post.


You mentioned JSP as the way you're generating the XML. Does your JSP declare the content type in its <%@ page %> declaration?
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just found a train company I use that has one (this was surprisingly hard to find one when needed lol) http://www.tpexpress.co.uk/sitemap.xml - if you open this up in say Google Chrome you can see the sitemap looks pretty as the browser is rendering visuals.

I have also tried setting the content type in the JSP file although I get the following error message when loading the page;

This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.


This is the code behind the page;

<%@page contentType="text/xml"%>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<%=request.getAttribute("xmlsitemap")%>

So basically the issue is with the first line of the code which is setting the content type within the JSP



Thanks
Michael
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Cropper wrote:Just found a train company I use that has one (this was surprisingly hard to find one when needed lol) http://www.tpexpress.co.uk/sitemap.xml - if you open this up in say Google Chrome you can see the sitemap looks pretty as the browser is rendering visuals.


Really? In all my browsers I just get a utilitarian dump of the XML contents. Not pretty at all.

I have also tried setting the content type in the JSP file although I get the following error message when loading the page;

[i]This page contains the following errors:

error on line 2 at column 6: XML declaration allowed only at the start of the document


You can declare the document's encoding in your <%@ page %> declaration as well. I would omit the XML prolog and let the JSP generate it if it can. Remember that when you send XML via HTTP, the actual encoding for the document comes from the HTTP header and not from the XML prolog, even if you provide a prolog.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is my version of pretty :-) As I am comparing it to what I am seeing which is just purely the data within the tags and not with the nodes wrapped around.

So I am seeing for example;
www.website.com

Whereas what I would expect to see is;
<loc>www.website.com</loc>

I have removed the setContentType line of code from the Servlet and just put the <%@page contentType="text/xml"%> into the JSP and it is still the same error.

This is very strange. It is getting late now so I will pick this one back up over the weekend when I can create a mini version to test with as it may be something with the way that the data is being placed on the request attribute which is causing the browser difficulty in reading it.

Will keep you updated.


Thanks
Michael
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. If it were me, at this point I would get rid of the JSP and just have your servlet output the XML to the response. One less moving part to break.
 
Michael Cropper
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really was that simple! It's working perfect now.

Previously I had the following code within my servlet;



So I removed that and simply had the following code instead (the variable 'xmlsitemap' is the long String that was built as I explained easlier)



It really was as simple as that to fix - took me ages the other night trying different things.

What I can take from this is that is appears that JSP files override any 'Content Type' headers that have been set within the JSP and default back to "text/html". I guess this is one of the ways Java tries to simplify the process of outputting content by having one less thing to think about as the JSP handles this. Good to know for future at least.


Thanks for all the help as usual :-)


Michael
 
Arik Cohen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a generator like http://create-site-map.com/ instead?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic