Reverse XML tags using java

Many times we need to reverse the xml tag

</Request>
</name>
</address>
<pincode>123456</pincode>
<area>ABC</area>
<flat>XYZ</flat>
<address>
<name>
<Request>

as below :

<Request>
<name>
<address>
<flat>XYZ</flat>
<area>ABC</area>
<pincode>123456</pincode>
</address>
</name>
</Request>

 

Steps:

 

reverserequest

  1. Create a xml with reverse request
  2. Click on ReverseXMLrequest.bat

 

      3. Enter the source file and destination file path

reverserequest_batch

4) The File with correct xml will be generated in destination path.

 

Code:

==================================

  1. Create a Mavenized project.
  2. Add dependencies jars like commons-io
  3. pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.subhasish</groupId>
<artifactId>ReverseXMLRequest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url&gt;
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.subhasish.ReverseXMLRequest</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>ReverseXMLRequest</finalName>
</build>
</project>

 

4) ReverseXMLRequest.java

package com.subhasish;

import org.apache.commons.io.input.*;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import javax.xml.transform.*;
import javax.xml.parsers.*;
public class ReverseXMLRequest {

public static void main(String[] args) throws ParserConfigurationException,IOException,SAXException,TransformerException{
String ch;
String destinationPath=args[1];
String tempPath=”C://temp.xml”;
try(ReversedLinesFileReader fr = new ReversedLinesFileReader(new File(args[0]));)
{
Path path = Paths.get(tempPath);
Files.deleteIfExists(path);
Files.createFile(path);
do {
ch = fr.readLine();
Files.write(path, ch.getBytes(),StandardOpenOption.APPEND);

} while (ch != null);
}
catch(Exception e)
{
formatXML(tempPath,destinationPath);
}}

public static void formatXML(String path,String destinationPath) throws IOException
{
Path deletePath = Paths.get(path);
try
{
final InputSource src = new InputSource(new FileInputStream(new File(path)));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation(“LS”);
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter(“format-pretty-print”,Boolean.TRUE);
LSOutput lsOutput = impl.createLSOutput();
lsOutput.setEncoding(“UTF-8”);
Writer stringWriter = new StringWriter();
lsOutput.setCharacterStream(stringWriter);
writer.write(document, lsOutput);
String result = stringWriter.toString();
Path formattedpath = Paths.get(destinationPath);
Files.createFile(formattedpath);
Files.deleteIfExists(deletePath);
Files.write(formattedpath, result.getBytes(),StandardOpenOption.APPEND);
System.out.println(“DONE”);
} catch (Exception e)
{
Files.deleteIfExists(deletePath);
}
}}

 

5)  ReverseXMLrequest.bat

@echo off
set /p sourceinput=”Enter Source Path::”
set /p destinput=”Enter Destination Path::”
java -jar ReverseXMLRequest.jar %sourceinput% %destinput%
pause

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s