You are here:   HomeJava ProgrammingPackagesxml parsers Java Example
by Dinesh Thakur Category: Packages

 

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




Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular