Convert String to XML file in Java.

Hello Friends,

In the previous example we have seen how to convert XMl to String. Now this example will do the exact opposite of the previous one i.e. convert from String to XML. We will now try to convert the String stream into an XML file object. Here is the simple code for that.

[java]

package cyb.others;

import java.io.File;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class StringToXML {
public static void main(String []args)
{
String xmlToString = “”;
DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder;
try
{
docbuilder = docfactory.newDocumentBuilder();
Document stringDocument = docbuilder.parse( new InputSource(new StringReader(xmlToString)));
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer transformer = tranFactory.newTransformer();

//Uncomment this if you need to remove the line –
//< ?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>

//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, “yes”);

//Uncomment this is you want to remove – standalone=”no”
//stringDocument.setXmlStandalone(true);

Source source = new DOMSource(stringDocument);
Result destination = new StreamResult( new File(“newCreatedFile.xml”));
transformer.transform( source, destination );

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

[/java]

Here, in the above code there are some commented data, once you uncomment it you can see the changes in the xml file. Hope this will help you.

Regards,

Nikhil Naoghare.

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.