by Dinesh Thakur

 

import javax.xml.parsers.*;

import org.w3c.dom.*;
import java.io.*;
public class JavaExampleParser
   {
       static String TxtToDisp[] = new String[1000];
       static int TxtLines = 0;
       public static void parseDocument(String FileNme)
        {
         try
            {
               DocumentBuilderFactory DocBf =DocumentBuilderFactory.newInstance();
               DocumentBuilder DocBldr = null;
               DocBldr = DocBf.newDocumentBuilder();
               Document Doc = null;
               Doc = DocBldr.parse(new File(FileNme));
               createDisplay(Doc,"");
            }
              catch (Exception e1)
            {
                   e1.printStackTrace(System.err);
                }
        }
              public static void createDisplay(Node Nd,String Indnt)
                 {
                    if (Nd==null)
                      {
                         return;
                      }
                         int Typ= Nd.getNodeType();
                         switch(Typ)
                            {
                              case Node.DOCUMENT_NODE:
                          {
                                     TxtToDisp[TxtLines] = Indnt;
                                     TxtToDisp[TxtLines] += "<?xml version=\"1.0\" encoding=\"" + "UTF-8" + "\"?>";
                                     TxtLines++;
                                     createDisplay(((Document)Nd).getDocumentElement(), "");
                                     break;
                                  }
                                    case Node.ELEMENT_NODE:
                               {
                                          TxtToDisp[TxtLines] = Indnt;
                                          TxtToDisp[TxtLines] += "<";
                                          TxtToDisp[TxtLines] += Nd.getNodeName();
                                          int NmbrAttrbute = 0;
                                          if(Nd.getAttributes() != null) NmbrAttrbute= Nd.getAttributes().getLength();
                                          for (int loopIndx = 0; loopIndx <NmbrAttrbute;loopIndx++)
                                      {
                                                 Attr Atrbute = (Attr)Nd.getAttributes().item(loopIndx);
                                                 TxtToDisp[TxtLines] += " ";
                                                 TxtToDisp[TxtLines] += Atrbute.getNodeName();
                                                 TxtToDisp[TxtLines] += "=\"";
                                                 TxtToDisp[TxtLines] += Atrbute.getNodeValue();
                                                 TxtToDisp[TxtLines] += "\"";
                                              }
                                                 TxtToDisp[TxtLines] += ">";
                                                 TxtLines++;
                                                 NodeList ChldNds = Nd.getChildNodes();
                                                 if (ChldNds != null)
                                       {
                                                      int NmbrChldrn = ChldNds.getLength();
                                                      Indnt += "    ";
                                                      for (int loopIndx = 0; loopIndx < NmbrChldrn;loopIndx++ )
                                                       {
                                                         createDisplay(ChldNds.item(loopIndx),Indnt);
                                                       }
                                                   }
                                                       break;
                                        }
                                                case Node.TEXT_NODE:
                                        {
                                                        TxtToDisp[TxtLines] = Indnt;
                                                        String NdTxt = Nd.getNodeValue().trim();
                                                        if(NdTxt.indexOf("\n") < 0 && NdTxt.length() > 0)
                                             {
                                                             TxtToDisp[TxtLines] += NdTxt;
                                                             TxtLines++;
                                                         }
                                                             break;
                                                    }
                                                       case Node.PROCESSING_INSTRUCTION_NODE:
                                              {
                                                               TxtToDisp[TxtLines] = Indnt;
                                                               TxtToDisp[TxtLines] += "<?";
                                                               TxtToDisp[TxtLines] += Nd.getNodeName();
                                                               String PITxt = Nd.getNodeValue();
                                                               if (PITxt != null && PITxt.length() > 0)
                                                 {
                                                                    TxtToDisp[TxtLines] += PITxt;
                                                                 }
                                                                    TxtToDisp[TxtLines] += "?>";
                                                                    TxtLines++;
                                                                    break;
                                                          }   
                        }
                                if (Typ == Node.ELEMENT_NODE)
                           {
                                       TxtToDisp[TxtLines] =Indnt.substring(0,Indnt.length()-4);
                                       TxtToDisp[TxtLines] += "</";
                                       TxtToDisp[TxtLines] += Nd.getNodeName();
                                       TxtToDisp[TxtLines] += ">";
                                       TxtLines++;
                                       Indnt += "    ";
                                  }
                 }  
                        public static void main(String aa[])
                                  {
                                      parseDocument(aa[0]);
                                      for(int loopIndx = 0; loopIndx < TxtLines; loopIndx++)
                                 {
                                             System.out.println(TxtToDisp[loopIndx]);
                                         }
                                 }
  }
xml parsers Java Example