In this section, you will learn how to create xml file using Servlet We have created file XmlServlet.java. It creates XML file with its version and encoding and display a message 'Xml File Created Successfully'.
JAXP (Java API for XML Processing) is a Java interface that provides a standard approach to Parsing XML documents. With JAXP, we will use the Document BuilderFactory to create DocumentBuilder class. The class DocumentBuilderFactory is responsible for creating new DOM .
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory .newDocumentBuilder();DocumentBuilderFactory is used to find the class to load.
The instance of the class DocumentBuilder is used to create a blank document.
Document doc = docBuilder.newDocument();
Step 1: Create a Servlet "XmlServlet.java"
import java.io.*;
|
Step 2:Create a "Web.xml" for mapping the Servlet "XmlServlet.java".
|
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <servlet> <servlet-name>xmlservlet</servlet-name> <servlet-class>XmlServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xmlservlet</servlet-name> <url-pattern>/xmlServlet</url-pattern> </servlet-mapping> </web-app> |
Output :
![]()
"newxml.xml"

|
Recommend the tutorial |

Ask Questions? Discuss: Create XML File using Servlet View All Comments
Post your Comment