Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

servlet with xml extension not working

Avatar

Level 6

Hi Team,

 

I tried servlet with extension as xml as below ,but it's not working 

@Component(service=Servlet.class,
property={
        Constants.SERVICE_DESCRIPTION+"=Servlet to render asset data in xml",
        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=mysite/components/text",
        ServletResolverConstants.SLING_SERVLET_METHODS + "=GET",
        ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=xml",
        ServletResolverConstants.SLING_SERVLET_SELECTORS + "=index"
})
public class SiteMapIndexServlet extends SlingAllMethodsServlet {

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml");
        ResourceResolver resourceResolver = request.getResourceResolver();
        String path = request.getRequestParameter("path").toString();
        response.getWriter().print("hello World!!");
        response.setStatus(SlingHttpServletResponse.SC_OK);
    }
}
 
//It is showing below error

This page contains the following errors:

error on line 1 at column 1: Document is empty

Below is a rendering of the page up to the first error.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @Keerthi0555 ,

The problem is the incomplete XML file format you want to print. That's why it gives an error. To solve this you can follow this code snippet which will help you to complete your sitemap code,

    private static final String NS = "http://www.sitemaps.org/schemas/sitemap/0.9";

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws IOException {

        final XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
        try {
            final XMLStreamWriter stream = outputFactory.createXMLStreamWriter(response.getWriter());
            stream.writeStartDocument("1.0");
            stream.writeStartElement("", "urlset", NS);
            stream.writeNamespace("", NS);

            // Loop & Make you Sitemap code
            stream.writeStartElement(NS, "url");
            this.writeElement(stream, "loc", request.getRequestParameter("path").toString());

            stream.writeEndElement();
            stream.writeEndDocument();
            response.setContentType("text/xml");
        } catch (XMLStreamException e) {
            log.error("Error");
        }
    }
    private void writeElement(final XMLStreamWriter stream, final String elementName, final String text) throws XMLStreamException {
        stream.writeStartElement(NS, elementName);
        stream.writeCharacters(text);
        stream.writeEndElement();
    }

After running this code you will get the following output,

Sady_Rifat_0-1691048272765.png

Now, you can change your code based on your requirement.

I hope this helps you.

View solution in original post

4 Replies

Avatar

Employee

you need to provide XML structure of the response. example - 

<?xml version="1.0" encoding="UTF-8"?>
<aem>
  <message>Hello world</message>
</aem>

Avatar

Correct answer by
Community Advisor

Hello @Keerthi0555 ,

The problem is the incomplete XML file format you want to print. That's why it gives an error. To solve this you can follow this code snippet which will help you to complete your sitemap code,

    private static final String NS = "http://www.sitemaps.org/schemas/sitemap/0.9";

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws IOException {

        final XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
        try {
            final XMLStreamWriter stream = outputFactory.createXMLStreamWriter(response.getWriter());
            stream.writeStartDocument("1.0");
            stream.writeStartElement("", "urlset", NS);
            stream.writeNamespace("", NS);

            // Loop & Make you Sitemap code
            stream.writeStartElement(NS, "url");
            this.writeElement(stream, "loc", request.getRequestParameter("path").toString());

            stream.writeEndElement();
            stream.writeEndDocument();
            response.setContentType("text/xml");
        } catch (XMLStreamException e) {
            log.error("Error");
        }
    }
    private void writeElement(final XMLStreamWriter stream, final String elementName, final String text) throws XMLStreamException {
        stream.writeStartElement(NS, elementName);
        stream.writeCharacters(text);
        stream.writeEndElement();
    }

After running this code you will get the following output,

Sady_Rifat_0-1691048272765.png

Now, you can change your code based on your requirement.

I hope this helps you.

Avatar

Community Advisor

Hello @Keerthi0555 

 

The error message "error on line 1 at column 1: Document is empty" typically indicates that the XML response received by the AEM) Servlet is empty or not well-formed.

 

Can you try with this please

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(service=Servlet.class,
property={
        Constants.SERVICE_DESCRIPTION+"=Servlet to render asset data in xml",
        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=mysite/components/text",
        ServletResolverConstants.SLING_SERVLET_METHODS + "=GET",
        ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=xml",
        ServletResolverConstants.SLING_SERVLET_SELECTORS + "=index"
})
public class SiteMapIndexServlet extends SlingAllMethodsServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/xml");
        PrintWriter out = response.getWriter();
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        out.println("<books>");
        out.println("    <book>");
        out.println("        <title>Book One</title>");
        out.println("        <author>Author One</author>");
        out.println("        <publication>Publication One</publication>");
        out.println("    </book>");
        out.println("    <book>");
        out.println("        <title>Book Two</title>");
        out.println("        <author>Author Two</author>");
        out.println("        <publication>Publication Two</publication>");
        out.println("    </book>");
        out.println("</books>");
        response.setStatus(SlingHttpServletResponse.SC_OK);
    }
}

 


Aanchal Sikka