Skip to end of metadata
Go to start of metadata


Applies to:

SAP PI Developers and functional consultants. 

Summary

Often PI developer want to convert a un-indent XML file into indent file.

Un-indent XML: - entire XML is in single line OR is in a format which is difficult to understand.

Indent XML: - properly formatted XML, which is easy to read and understand. 

Author(s):  

   
Name: Raghu Vamseedhar Reddy .K

Company:     Infosys Limited.
Created on:    8/29/2011
Author(s) Bio

I am SAP Certified Development Associate - Process Integration with SAP Netweaver (PI 7.1). I have about 5 year experience of SAP XI/PI implementation and support. I am also, Sun Certified Programmer for the Java Platform, Standard Edition 6. I have good understanding of XML, XSLT, SOAP, WSDL, ABAP. I am an active member in SAP SDN. 

Here are options through which un-indent XML file can be converted to indent XML

  1. Using internet browsers: Open XML file using IE or Chrome you will partially indent file, i.e., child elements will not be preceded by spaces. If you copy paste from IE, you might need to replace all “ - < ” with <. (Note: Do not replace “ - “ with nothing, you might replace “ - “ in data). If you are opening huge file (5MB), internet browser may become unresponsive, safe size of XML depends on your computer resources.
  2. Using ESR: - Open any message mapping, test tab, XML source, there paste un-indent XML and then press pretty print. If you are opening huge file (5MB), ESR may become unresponsive.
  3. Using Microsoft XML Notepad: - open un-indent XML file with XML Notepad. Click on save, it will indent XML file.
  4. Using Java IDE: Open the XML file (.xml) with Java IDE. In NetBeans, source -> format (Alt + Shift + F), in* Eclipse*, source -> format (Ctrl + Shift + F).
  5. Using commercial XML editors: - Commercial XML editors, like XMLSpy, Stylus Studio, have option to indent XML files.
  6. Using attached Java swing based application. Note: - Make an executable Jar file and save it on desktop, so that you can use frequently.

 

JFrame_IndentXML
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrame_IndentXML.java
 *
 * Created on Jun 14, 2011, 8:46:21 PM
 */
package com.xml.indent;

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author Raghu_Reddy01
 */
public class JFrame_IndentXML extends javax.swing.JFrame {

    /** Creates new form JFrame_IndentXML */
    public JFrame_IndentXML() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel_Input = new javax.swing.JPanel();
        jLabel_Input = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane_Input = new javax.swing.JEditorPane();
        jEditorPane_Input.setText("<root><C1><GC1>gc1</GC1><GC2>gc2</GC2></C1><C2><GC3>gc3</GC3></C2><C3><GC4><GGC1>ggc1</GGC1></GC4></C3></root>");
        jPanel_Output = new javax.swing.JPanel();
        jLabel_Output = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane_Output = new javax.swing.JEditorPane();
        jButton_IndentXML = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Indent XML");

        jSplitPane1.setDividerLocation(120);
        jSplitPane1.setDividerSize(10);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        jLabel_Input.setText("  Input");

        jScrollPane1.setViewportView(jEditorPane_Input);

        javax.swing.GroupLayout jPanel_InputLayout = new javax.swing.GroupLayout(jPanel_Input);
        jPanel_Input.setLayout(jPanel_InputLayout);
        jPanel_InputLayout.setHorizontalGroup(
            jPanel_InputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_InputLayout.createSequentialGroup()
                .addComponent(jLabel_Input)
                .addContainerGap(366, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
        );
        jPanel_InputLayout.setVerticalGroup(
            jPanel_InputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_InputLayout.createSequentialGroup()
                .addComponent(jLabel_Input)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
        );

        jSplitPane1.setTopComponent(jPanel_Input);

        jLabel_Output.setText("Output");

        jScrollPane2.setViewportView(jEditorPane_Output);

        jButton_IndentXML.setText("Indent XML");
        jButton_IndentXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_IndentXMLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel_OutputLayout = new javax.swing.GroupLayout(jPanel_Output);
        jPanel_Output.setLayout(jPanel_OutputLayout);
        jPanel_OutputLayout.setHorizontalGroup(
            jPanel_OutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_OutputLayout.createSequentialGroup()
                .addComponent(jLabel_Output)
                .addGap(18, 18, 18)
                .addComponent(jButton_IndentXML, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
        );
        jPanel_OutputLayout.setVerticalGroup(
            jPanel_OutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_OutputLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_OutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_Output)
                    .addComponent(jButton_IndentXML))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(jPanel_Output);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void jButton_IndentXMLActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        try {
            Source xmlInput = new StreamSource(new StringReader(jEditorPane_Input.getText()));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setAttribute("indent-number", 4);
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(xmlInput, xmlOutput);
            jEditorPane_Output.setText(xmlOutput.getWriter().toString());
        } catch (TransformerException ex) {
            jEditorPane_Output.setText("Error: " + ex.getLocalizedMessage() + " " + ex.getLocationAsString() + " " + ex.getMessageAndLocation());
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrame_IndentXML().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton_IndentXML;
    private javax.swing.JEditorPane jEditorPane_Input;
    private javax.swing.JEditorPane jEditorPane_Output;
    private javax.swing.JLabel jLabel_Input;
    private javax.swing.JLabel jLabel_Output;
    private javax.swing.JPanel jPanel_Input;
    private javax.swing.JPanel jPanel_Output;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration
}

2 Comments

  1. 7. Use foxe editor to indent XML. Foxe is very good editor for text and XML. Blog for more details.

  2. Probably  By far NOTEPAD++ is the best editor for multiple text best application.

    If you use XMLTool Plugin you can do a lot of stuffs.