Home Discussion Discuss: Reading XML from a File

  • xml and javarahulrishi March 21, 2012 at 11:09 PM

    import java.io.*; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.*; import org.w3c.dom.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.xml.sax.SAXException; public class TestingServlet extends HttpServlet { String name1=null; String requestXmlFile1=null; String requestPage1=null; String requestProviderClass1=null; String responseXmlFile1=null; String responsePage1=null; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); name1 = request.getParameter("Name"); requestXmlFile1 = request.getParameter("RequestXMLFile"); requestPage1 = request.getParameter("RequestPage"); requestProviderClass1 = request.getParameter("RequestProviderClass"); responseXmlFile1= request.getParameter("ResponseXMLFile"); responsePage1 = request.getParameter("ResponsePage"); try{ out.println(4); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); out.println(3); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); out.println(1); Document document = documentBuilder.newDocument(); out.println(1); document = documentBuilder.parse("Services.xml"); out.println(6); Element rootElement = document.createElement("Services"); document.appendChild(rootElement); Element service = document.createElement("Service"); Element name = document.createElement("Name"); name.appendChild(document.createTextNode("Search")); service.appendChild(name); Element requestXmlFile=document.createElement("RequestXMLFile"); requestXmlFile.appendChild(document.createTextNode("SearchRequest.xml")); service.appendChild(requestXmlFile); Element requestPage=document.createElement("RequestPage"); requestPage.appendChild(document.createTextNode("http://localhost:8084/WebServiceForFrontech/SearchProduct.jsp")); service.appendChild(requestPage); Element requestProviderClass=document.createElement("RequestProviderClass"); requestProviderClass.appendChild(document.createTextNode("SearchServlet")); service.appendChild(requestProviderClass); Element responseXmlFile=document.createElement("ResponseXMLFile"); responseXmlFile.appendChild(document.createTextNode("SearchResponse.xml")); service.appendChild(responseXmlFile); Element responsePage=document.createElement("ResponsePage"); responsePage.appendChild(document.createTextNode("http://localhost:8084/WebServiceForFrontech/SearchPoductResult.jsp")); service.appendChild(responsePage); rootElement.appendChild(service); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new FileOutputStream("Services.xml")); transformer.transform(source,result ); out.println(5); }catch(Exception e){} } public void addNewService() throws IOException ,ParserConfigurationException , SAXException , TransformerConfigurationException , FileNotFoundException , TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement("Services"); document.appendChild(rootElement); Element service = document.createElement("Service"); Element name = document.createElement("Name"); name.appendChild(document.createTextNode("Search")); service.appendChild(name); Element requestXmlFile=document.createElement("RequestXMLFile"); requestXmlFile.appendChild(document.createTextNode("SearchRequest.xml")); service.appendChild(requestXmlFile); Element requestPage=document.createElement("RequestPage"); requestPage.appendChild(document.createTextNode("http://localhost:8084/WebServiceForFrontech/SearchProduct.jsp")); service.appendChild(requestPage); Element requestProviderClass=document.createElement("RequestProviderClass"); requestProviderClass.appendChild(document.createTextNode("SearchServlet")); service.appendChild(requestProviderClass); Element responseXmlFile=document.createElement("ResponseXMLFile"); responseXmlFile.appendChild(document.createTextNode("SearchResponse.xml")); service.appendChild(responseXmlFile); Element responsePage=document.createElement("ResponsePage"); responsePage.appendChild(document.createTextNode("http://localhost:8084/WebServiceForFrontech/SearchPoductResult.jsp")); service.appendChild(responsePage); rootElement.appendChild(service); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new FileOutputStream("Services.xml")); transformer.transform(source,result ); } } this code doent work properly please help me

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Development process Questions

questions from an xml file
questions from an xml file  i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one 

Java Beginners

How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm 

XML Questions

data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou 

JavaScript Questions

graph generation from xml design file
graph generation from xml design file  how to search words in XML file with javascript. i have copied contents of XML file into word file. using searched keywords i would like to draw graph 

XML Questions

How to create XML file - XML
language has many API's to create xml file from program.Here is the code example...()); }}Thanks  Hi,Java programming language has many API's to create xml file from...How to create XML file  Creating a XML file, need an example. Thanks 

JDBC Questions

xml file creation in java
xml file creation in java  how to create xml file in java so that input should not be given from keyboard. and that file should be stored.   Please visit the following links: http://www.roseindia.net/tutorial/java/xml 

XML Tutorials

Reading XML from a File
Reading XML from a File       This Example shows you how to Load Properties from the XML file via... to create new DOM parsers. Some of the methods used for reading XML from a  

XML Tutorials

Loading properties from a XML file
Loading properties from a XML file       This Example shows you how to Load properties from a XML... parsers. Some of the methods used for loading properties from a XML file 

Ajax Questions

retrieve in xml file in struts2
can i get back the value from the xml file in the ajax function and print...retrieve in xml file in struts2  i am using struts2 and trying... properties file using ajax. i now put the values from properties file to map 

JDBC Questions

xml configuration file - JDBC
xml configuration file  Hi, Could you please tell me how to write a xml configuration file . We have mysql database in some other system. I have... to create a xml file. Please help me out. Thank you  Hi Friend, Try 

XML Tutorials

Get Data From the XML File
Get Data From the XML File       Here you will learn to retrieve data from XML file using SAX parser... from an array.   Here is the XML File: Employee-Detail.xml 

XML Questions

download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code 

Programming Tutorials

Passing values in ComboBox from XML file
Passing values in ComboBox from XML file In this tutorial we are going... from an XML document. For this what we need  a XML file in which we have... the data from the XML file and insert it into the ComboBox. To make a program over 

XML

JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... to write the Java Objects into a XML file and unmarshal() method to read XML 

XML Questions

create a xml from sql server 2005 - XML
create a xml from sql server 2005  hello Dear, i want to know how we create a xml file which retrieve data from Sql server 2005 using java. i... with the name as 'student' i want to create a xml file from the table values which 

XML Questions

parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following 

XML Questions

how to update xml from java - XML
how to update xml from java  hi, Im new to xml parsing and dont know much about. I need to modify the attribute val of a tag in a complex xml file.... Suppose we have one xml file named "document.xml" document.xml 

JSP Servlet Questions

Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml 

Struts Questions

Sava data from Form to XML file using strutrs
Sava data from Form to XML file using strutrs  I'am a biginner with struts want so save data from my form in an Xml file using struts but i'm searching witout finding a solution thanks fo your help 

Swing AWT

Display data from xml file to Swings - Swing AWT
Display data from xml file to Swings  Hi, We are Preparing a stand alone application. Where the Swings is the front end. There will be only... clicking the buttons in swings it has to display 20 record in one shot in table 

XML Questions

Create XMl dynamically - XML
Create XMl dynamically  Hi I am retreiving the list from database which i need to display in an XML file with some nodes How can I do 

General

Java get XML File
Java get XML File     ... the XML file. For this, you need to create a XML file. Here is the employee.xml file: <?xml version="1.0"?> <company> 

JDBC Questions

Fetch Records from SQL database and convert into XML file
Fetch Records from SQL database and convert into XML file  Hi Experts... file with all records into a single XML with containing as Row's. Please see the output XML: - I need output XML File as: - ** <?xml version="1.0 

XML Tutorials

Getting Data from XML File (Document)
Getting Data from XML File (Document)   ... from a XML file. All xml files store the data. You can add and modify the data...: This program helps you in retrieving the data from a XML file. It takes a xml file 

Java Beginners

xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers. 

JavaScript Questions

Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions 

XML Questions

Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file 

JSP Tutorials

Retrieving Data From the XML file
Retrieving Data From the XML file  ....  By this example we are going to get the XML data from the xml file in our... is to retrieve the data from the xml file. This xml file has all the information about 

Java Beginners

Writing xml file - Java Beginners
XmlServlet().createXmlTree(doc); System.out.println("Xml File Created... from xml tree StringWriter sw = new StringWriter...Writing xml file  Thank you for the quick response The values which 

General

Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... have developed an application to create xml file from flat file and data...). Create a file "FlatFileXml.java"  used to create an XML and data 

DMCA.com