Thursday, June 11, 2009

Transforming XML into another format of XML based on a XSL using Java

This article will enlighten us with the transformation of XML from one format to another format. If you have a definite XML (SampleInputXML.xml) and definite XSL (SampleXSL.xsl) which is suitable to change format, the below code will bring you the XML with definite format.


The code is given below.


import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;


public class XMLTransformation {


XMLTransformation() {

}


private static void applyTransformAndSave() {
try {
String sampleInputXML= "SampleInputXML.xml";
File sampleInputFile = new File(sampleInputXML);
String sampleOutputXML = "SampleOutputFile.xml";
//Convert input file to output batch file according to the XSL
TransformerFactory tFactory1 = TransformerFactory.newInstance();
Transformer transformer1;
transformer1 = tFactory1.newTransformer(new StreamSource("SampleXSL.xsl"));
transformer1.setOutputProperty(OutputKeys.INDENT, "yes");
transformer1.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer1.transform(new StreamSource(sampleInputFile), new StreamResult(new FileOutputStream(sampleOutputXML)));
} catch (TransformerConfigurationException tCEx){
tCEx.printStackTrace();
} catch (TransformerException tEx){
tEx.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
}



public static void main(String args[]) {
XMLTransformation xmlTransformation = new XMLTransformation();
XMLTransformation.applyTransformAndSave();
}

}



Sample Input XML file:

       <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
              <Order>
         <CustomerID>0123456789</CustomerID>
         <Title>Mr</Title>
           <FirstName>Vicky</FirstName>
            <Initial>VV</Initial>
           <Surname>Verma</Surname>
             <TelephoneNumber>0987654321</TelephoneNumber>
              <SubPremise>RakhsakNagar</SubPremise>
            <BuildingName>RakhsakNagar</BuildingName>
             <StreetNumber>207</StreetNumber>
             <StreetName>KharadiByPass</StreetName>
             <Locality>Kharadi</Locality>
           <PostTown>Pune</PostTown>
          <County>Pune</County>
         <Postcode>411014</Postcode>
        <OrderDate>20052009 15:18:11</OrderDate>
       <CADDate>05062009 15:23:54</CADDate>
       <OrderReference>022-123456789</OrderReference>
        <DealerChannel>Airtel Voice</DealerChannel>
       <InstallationType>Self</InstallationType>
     <AssetDescription>Airtel TV</AssetDescription>
     <Action>Add</Action>
    <ReplacementType>Active</ReplacementType>
    <CurrentDate>20090611T11:15:08</CurrentDate>
  </Order>




Sample Input XSL File:

    <?xml version="1.0" encoding="UTF-8" ?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:strip-space elements="*" />
     <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes" />
<xsl:template match="Order">
      <OrderForProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <UniqueKey>
      <xsl:attribute name="Id">
     <xsl:value-of select="CustomerID" />
  </xsl:attribute>
  </UniqueKey>
      <ProductsDetails>
      <Product Name="Airtel_TV" />
  </ProductsDetails>
      <PriorSubmission>
      <xsl:attribute name="UserName">AirtelUser</xsl:attribute>
      <xsl:attribute name="SubmitterRef">
     <xsl:value-of select="OrderReference" />
  </xsl:attribute>
  </PriorSubmission>
  </OrderForProduct>
  </xsl:template>
  </xsl:stylesheet>





Sample Output XML file:

   <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
     <OrderForProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <UniqueKey Id="0123456789" />
        <ProductsDetails>
       <Product Name="Airtel_TV" />
    </ProductsDetails>
     <PriorSubmission UserName="AirtelUser" SubmitterRef="022-123456789" />
   </OrderForProduct>



Thanks.

No comments:

Total Pageviews