Converting XML to string

Ads
 

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 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


August 13, 2015 at 5:26 AM

Ads









Related Tutorials/Questions & Answers:
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
Converting ISO8601-compliant String to java.util.Date
Converting ISO8601-compliant String to java.util.Date  Converting ISO8601-compliant String to java.util.Date
Advertisements
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
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
converting an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
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
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 a String to Integer in Java
Converting a String to Integer in Java In this video tutorial I will show you.... There are two methods of converting the String into integer value. One method... is example code for converting a string to Integer: String strNoOfItems = "
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 Boolean to String
Converting Boolean to String In this section we will discuss about how to convert boolean to String type. This example show you how to convert boolean type... of data and converting into String type which prints the string value
Converting object to String
Converting object to String In this section you will learn to convert Object to String in java. It is sometimes necessary to convert object to String because you need to pass it to the method that accept String only. Object is a class
Convert a String into an Integer Data
Convert a String into an Integer Data In this section you will learn to convert a string type of data to integer type data. Converting string to integer and integer to string is a basic task in java programming language because
temperature converting
))); } } public static void main(String[] args) { displayTable
XML
XML  How i remove a tag from xml and update it in my xml
How to convert String Date to Timestamp in Java?
* Example of Converting String date * into timestamp from a Java program... for converting String Date into Timestamp. Here are few more related tutorials related...String Date to Timestamp example in Java Java is programming language
xml
xml  why the content written in xml is more secure
xml
xml  validate student login using xml for library management system
xml
xml  validate student login using xml for library management system
xml
xml  what is name space,xml scema give an example for each   XML Namespaces provide a method to avoid element name conflicts.They are used for providing uniquely named elements and attributes in an XML document
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
XML
XML  please tell me how i remove one tag out of all similar type of tags in xml
xml
xml  how can i remove white space and next line when i copy stream to xml file
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
xml
xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0... gratis open standards. The design goals of XML emphasize simplicity, generality
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function

Ads