Hi Team,
I tried servlet with extension as xml as below ,but it's not working
Solved! Go to Solution.
Views
Replies
Total Likes
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,
Now, you can change your code based on your requirement.
I hope this helps you.
you need to provide XML structure of the response. example -
<?xml version="1.0" encoding="UTF-8"?>
<aem>
<message>Hello world</message>
</aem>
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,
Now, you can change your code based on your requirement.
I hope this helps you.
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);
}
}
Views
Likes
Replies