Home Answers Viewqa WebSevices Converting XML to string

 
 


Lisha
Converting XML to string
1 Answer(s)      2 years and 2 months ago
Posted in : WebSevices

Hi Friends,

I had an requirement in which a java program receives an xml messages from external web application such as (jsp, php) and then it converts the received xml message into a string.

could some one suggest how can I go about it or could some one provide me with sample code so that I can study from it.

Thanks in advance.

Regards, Lisha Ahuja

View Answers

March 5, 2011 at 9:12 AM


Hi,

You can use the Transformer class:

Transformer tFormer = TransformerFactory.newInstance().newTransformer();

For transforming the Document object into string.

XML file:

<?xml version = "1.0" ?>
<accounts>
    <account>
    <id>454457858778</id>
    <name>Dinesh</name>
    </account>
</accounts>

Java code:

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class XMLtoString{
  static public void main(String[] args){
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("1.xml");
        StringWriter stringWriter = new StringWriter(); 
        Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
        transformer.transform(new DOMSource(doc), new StreamResult(stringWriter)); 
        String strFileContent = stringWriter.toString(); //This is string data of xml file
        System.out.println(strFileContent);

    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

Thanks









Related Pages:
Converting XML to string
Converting XML to string  Hi Friends, I had an requirement in which a java program receives an xml messages from external web application such as (jsp, php) and then it converts the received xml message into a string. could
xml Converting to java using JDOM
xml Converting to java using JDOM  Hello , I am new to java and JDom so i make a Xml file and i need help to read it from java using objects , my... and getter , i dont know how to differentiate that this for example: line in xml
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
Converting PDF in to XML
Converting PDF in to XML  I have to convert PDF into XMl without any loss in text. Please suggest sth good
dom to xml string
for converting DOM object into string data. Please check the thread dom to xml... have save the dom document into xml file. How to convert dom to xml string...dom to xml string  Hi, I am writing a program in java that reads
Converting string to Int - JDBC
Converting string to Int  String nrate=request.getParameter("t3"); i want convert this nrate to integer to make calculations on it... so what... Friend, Use the following code to convert string to int. Integer i
Converting HTML to XML - Java Beginners
Converting HTML to XML  Hi, I am generating an HTML file (JSP from a Java Struts App.) I need to figure out a way to create an XML file from that HTML document. If you can refer me to some Java Code, that would
converting an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
Converting Anything to String
Java: Converting Anything to String Summary: Converting any data... for converting any object into a string. printf() was added in Java 5 to use a format... control using some of the alternatives. Converting numbers to strings - See
temperature converting
))); } } public static void main(String[] args) { displayTable
converting html to ppt,pptx - Java Beginners
converting html to ppt,pptx   Hi, i convert html to .doc format.But i want to convert the html to ppt,xml,pptx,docx,pdf.. Is there any...{ public static void main(String[]args){ try{ File file = new File("Hello.html
Converting a NSString into NSDate
Converting a NSString into NSDate  hello. How can I create a NSDate object out of it and then get different components like day, month, date, year from it. I have date string: Tuesday, October 19, 2010
Converting Numbers to Strings
documented, they are a better second choice. Converting Anything to String describes how to convert objects to String. Number to string conversion... Java: Converting Numbers to Strings Vanilla Java: Converting numbers
string manipulation in xml
string manipulation in xml  im working on build.xml using apache ant... in one of the targets i am assigning a var file with the full path of .sql files present in a folder. while executing that .sql file in xml its giving me
converting html to ppt,pptx - Java Beginners
converting html to ppt,pptx   Hi, i convert html to .doc format.But i want to convert the html to ppt,xml,pptx,docx,pdf.. Is there any possibility to solve this problem
converting html to ppt,pptx - Java Beginners
converting html to ppt,pptx   Hi, i convert html to .doc format.But i want to convert the html to ppt,xml,pptx,docx,pdf.. Is there any possibility to solve this problem
xml - XML
= stw.toString(); after i am getting xml string result, like Successxxx profile...xml  hi convert xml document to xml string.i am using below code... Friend, Please visit the following link: http://www.roseindia.net/xml/reading
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help... is converted into XML string."); } public void setParameter(Map param) { Map map
Converting a Filename to a URL
Converting a Filename to a URL       A file object is used to a give a filename. Creating the File...;static void main(String[] args){   File file=new File
Converting the text files into bzip file.
Converting text  file into bzip file In this example, you will see, how... a good programmer' along with converting a file into bzip file.  Opne the PHP...;text.bz2"; $string = " I am a good programmer.\n"; $bz2 = bzopen
XML - XML
)); System.out.print("Enter XML file name:"); String xmlFile = bf.readLine... of occurrences of each element type in an XML document and display them. The document file... String ele; public static void main(String args[])throws IOException
converting pdf to ps in mutiple languages - Development process
converting pdf to ps in mutiple languages  hi deepak, i want...). actually am converting the hindi pdf into hindi ps manuaaly, but i want... public void main(String args[]) throws Exception { try
Converting CDATA Nodes into Text Nodes
Converting CDATA Nodes into Text Nodes   ... to Text Nodes in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory
string - Java Beginners
Converting into String in Java  Need an example of Converting into String in JavaThanks in Advance
Converting Strings to Numbers
Java: Converting Strings to Numbers To convert a string value to a number (for example, to convert the String value in a text field to an int), use these methods. Assume the following declarations: String s; int i; long l; float
Converting PHP page with flash to JSP page without flash
Converting PHP page with flash to JSP page without flash  Hi, I... carried by xml. Now I want to convert this php page to jsp and not use flash... php file ? and the jsp file how can i access the xml data to display
how to write in xml? - XML
how to write in xml?  can anybody give the code how to write in xml... void main(String[] arg) { try{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter XML file name
converting From Hexadecimal to Decimal System - Java Interview Questions
converting From Hexadecimal to Decimal System  Write a program... { public static void main(String args[]) { int decimal=0; Scanner input=new Scanner(System.in); System.out.println("Enter a Roman Number: "); String
XML with JAVA - XML
XML with JAVA  Hi.. This is Priya here. Thanks for responding me. I... a particular node within XML document" . Explanation of program:- input must be in XML document and the processing in JAVA program . Find the time for each XML element
java and xml problem - XML
XML file: "); //String str = bf.readLine(); int no = Integer.parseInt("1...java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv
Create XML - XML
elements in your XML file: "); String str = buff.readLine(); int no = Integer.parseInt(str); System.out.print("Enter root: "); String root...(String[] args) throws Exception { BufferedReader buff = new BufferedReader(new
creating document in XML - XML
)); System.out.print("Enter number to add elements in your XML file: "); String...creating document in XML  Create an XML document for catalogue... CreatXMLFile { public static void main(String[] args) throws Exception
xml and xsd - XML
xml and xsd   50007812 2005-03-09T17:05:59... to use.i want to use in my local system and validate xml..kindly reply soon.if... { public static void main(String[] args) { try
storing data in xml - XML
storing data in xml  Can u plz help me how to store data in xml using...(Document doc,String name,String address,String contactNo,String email) throws...); transformer.transform(source, result); String xmlString = sw.toString
XML in database - XML
*; public class xml{ public static void main(String[] args) { try...XML in database  Hi Deepak, i m again facing problem with single element multiple tag in xml. i m trying to read the tag values into my
again with xml - XML
)); System.out.print("Enter a XML file name: "); String xmlFile = buff.readLine...again with xml  hi all i am a beginner in xml so pls give me... ElementNode { static public void main(String[] arg) { try
how to convert xml string to soap request object using axis1.4?
how to convert xml string to soap request object using axis1.4?  how to convert xml string to soap request object using axis1.4
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... org.w3c.dom.NodeList; class XMLReader { public static void main(String argv... final static String getElementValue( Node elem ) { Node node
Validating XML
Validating XML  Hi, I have a string containing data in XML format. I have to Validate this xml data. Is there any method in java that allows strings as input to validate xml?   Please visit the following link: http
java with xml
xml file in java DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
How to create XML file - XML
StreamResult(sw); transformer.transform(domSource, sr); String xml = sw.toString(); return xml; } public static void main(String args..., sr); String xml = sw.toString(); return xml; } public static
Java Glossary Term - D
that is used for converting the decimal numbers into Strings. This class is also used for parsing the Strings back in to binary numbers. It works with a String... Document Type Definition (DTD), defined in XML and SGML specifications is slightly
Creation of xml
and to create a xml file containing all those datas...My database datas are in key... of query select * from tablename where appid="+12345+". An xml file should be generated ...My xml file should in this format XXXX YYYY
readXML - XML
enter XML file name: "); String xmlFile = buff.readLine(); File...readXML   hiii i face problems in reading a xml file.. pixvalue1path1... in it... public ArrayList getImagePath(String [] kyid,String[] loc
XML and java
XML and java  Hi I have xml data in XMLStreamreader object how... static void main(String[] args) throws Exception { XMLInputFactory...(XMLStreamConstants.START_ELEMENT, null, "Emp_Id"); String id
DOM - XML
DOM  Hello.... I'm creating an xml file from java using DOM... from the database values... and saving it in a file But the created xml file...*; public class CreateXML{ public static void createXmlFile(Document doc,String
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file... org.w3c.dom.Node.*; public class MainClass { public static void main(String args
java - XML
in the XML document while i am parsing the file using SAX event based parser... Date(); public static void main(String[] args) throws IOException{ XMLParser detail = new XMLParser("C:/Person.xml"); } public XMLParser(String str
java - XML
java  how can i validate my xml file using java code plz send me de... kumar  Hi friend, Step to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. Here is the xml file "User.xml
creating tables as an xml document - XML
"); // create string from xml tree StringWriter sw = new StringWriter...creating tables as an xml document  Create a table of a medal tally of the Olympic Games as an XML document. The table must have the name

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.