Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Converting XML to string

Ads

 
 

Share on Google+Share on Google+

Lisha
Converting XML to string
2 Answer(s)      6 years and 5 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

Ads
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
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 an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
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
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 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 = "
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
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
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
string - Java Beginners
Converting into String in Java  Need an example of Converting into String in JavaThanks in Advance
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
temperature converting
))); } } public static void main(String[] args) { displayTable
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
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
converting field to text sql
converting field to text sql  I wanted to convert the field to TEXT in SQL.. is it possible?   SQL - Converting the field to TEXT works SELECT CONVERT(TEXT,fld_name) FROM TABLE_NAME   SQL - Converting
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
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
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
XML
XML  How i remove a tag from xml and update it in my xml
String
String  how to add spaces with string functions.?   Hi... String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n) { return
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
string
ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.nextLine(); String str[]=st.split(" "); for(int i=0