Read the last name Value from the testfile.xml
Dear All,
I have one requirement that, I have one xml testfile called testfile.xml , as shown in below.

Now , I am calling this firstname , lastname and number inside my servlet, by using below JAVA code.
import java.io.IOException;
import javax.jcr.Node;
import javax.servlet.Servlet;
import org.apache.http.HttpStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@8220494(service = Servlet.class)
@SlingServletResourceTypes(resourceTypes = { "wknd/components/form/search-unique" }, methods = {
HttpConstants.METHOD_POST }, extensions = "json")
public class SearchUniqueNumberServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = -8864935516234206658L;
private final Logger logger = LoggerFactory.getLogger(getClass());
ResourceResolverFactory resourceResolverFactory;
ResourceResolver resourceResolver;
/**
* @throws IOException
*
*/
@9944223
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
logger.info("INSIDE DO POST");
try {
resourceResolver = request.getResourceResolver();
Resource resource = resourceResolver.getResource("/content/dam/wknd/en/site/testfile.xml");
logger.info("resource INSIDE SEARCH ====== " + resource);
Node node = resource.adaptTo(Node.class);
String lastName = node.getProperty("LastName").getString();
logger.info("LastName INSIDE SEARCH ====== " + lastName);
} catch (Exception exception) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
response.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, exception.getMessage());
}
}
}
ISSUE - I am not able to fetch the lastname value from the XML file.
logger.info("LastName INSIDE SEARCH ====== " + lastName);
Below is my xml file.
************************** testfile.xml ************************
<?xml version="1.0" encoding="utf-8"?>
<Loans>
<LastUpdated><Time>2/13/2012</Time></LastUpdated>
<UniqueEntry>
<LastName>test1</LastName><PreferredName>sonu</PreferredName><UniqueNumber>1</UniqueNumber>
</UniqueEntry>
<UniqueEntry>
<LastName>test2</LastName><PreferredName>monu</PreferredName><UniqueNumber>2</UniqueNumber>
</UniqueEntry>
<UniqueEntry>
<LastName>test3</LastName><PreferredName>dinu</PreferredName><UniqueNumber>3</UniqueNumber>
</UniqueEntry>
</Loans>
Is it something I am not doing correctly ?