Hi all,


This is sri. I've done some program to read xml elements in java.But i didnt get them in a structured way like root element and its name and child elements and their names.I am getting end tag names after tag value also.so pls help me in getting elements in a proper way.

//java code
import java.io.File;
import java.io.FileInputStream; 
import java.nio.ByteBuffer; 
import java.nio.CharBuffer;
import java.nio.channels.FileChannel; 
import java.nio.charset.Charset; 
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; 
import java.util.regex.Pattern;



   public  class  XmlParse1 {
   static String mainPatternSrc = "(?s)\\G(?:" +"<!-.*?-->|" + "<\\?(.*?)\\?>|" + "<!\\[CDATA\\[(.*?)\\]\\]>|" + "<(/|)([\\w\\.\\-]+)([^>]*?)(/|)>|" + "([^<>]+))";
   static  Pattern  mainPattern  =  Pattern.compile(mainPatternSrc);

   private CharSequence seq; 
   private Matcher m;
   
   public XmlParse1(CharSequence seq) {
   this.seq = seq;
   }

   public boolean parseNext() { 
   if (m == null) {
   m = mainPattern.matcher(seq); 
   return true;
   }  
    else if (m.find()) {
      if (m.group(2) != null) {             
      text(m.group(2)); 
      }  
      else if(m.group(4)!=  null){
      if ("/".equals(m.group(3)))
      {  
      endElement(m.group(4));
      }
       else
       { 
       startElement(m.group(4));
       }
       } 
       else if(m.group(7) != null && !m.group(7).equals("")) { 
       text(translate(m.group(7)));
       }
       return true; 
       } 
       else{
       return false;
       }
      }
     
    static Map entityDefs = new HashMap(); 
     static {
     entityDefs.put("lt","<"); 
     entityDefs.put("gt",">"); 
     entityDefs.put("amp","&"); 
     entityDefs.put("quot","\""); 
     entityDefs.put("apos","'");
     }

  static  String  entityPatternSrc  =  "&#(\\d+);|&(\\w+);";
  static  Pattern  entityPattern  =  Pattern.compile(entityPatternSrc);
  
  private String translate(String s) { 
    Matcher m = entityPattern.matcher(s); 
     
    StringBuffer sb = null; 
    while(m.find()) {
      if(sb == null) 
      sb = new StringBuffer(); 
      if(m.group(1) != null) { 
         int i = Integer.parseInt(m.group(1)); 
         m.appendReplacement(sb,Character.toString((char) i)); 
         } 
         else {              
         String  entityStr  =  m.group(2);
         String entityVal = (String) entityDefs.get(entityStr); 
         if(entityVal != null) {
            m.appendReplacement(sb,entityVal); 
         } 
         else {
            throw  new  Error("Unkown  entity:  "+entityStr);
         }
        }
     }
      if(sb != null)  { 
         m.appendTail(sb); 
            
	return sb.toString();
       }
      else 
       { 
         return s;
       }
   }
    
    //events

      public void endElement(String s) 
      {
        //System.out.println(s);   
      }
      public  void  startElement(String  s)  
      {
        System.out.print(s+":");
      }
     public  void  text(String  s)
 
      {
         System.out.print(s);
      }
      


 public static void main(String[] args) throws Exception { 
  
  System.out.println(mainPatternSrc);
  File f = new File(args[0]);
  FileInputStream fis = new FileInputStream(f); 
  ByteBuffer bb =fis.getChannel().map(FileChannel.MapMode.READ_ONLY,0,f.length()); 
  CharBuffer cb = Charset.forName("UTF-8").decode(bb);

  XmlParse1 xp = new XmlParse1(cb); 
  while(xp.parseNext()) {
  }

 }

}

/* xml code
<?xml version="1.0" ?>

<Company>

     <Project>

       <title>Beacon</title>
       
       <frontend>php</frontend>
       
       <backend>mysql</backend>
       
       <client>Aircel</client>
       
       <team_size>20</team_size>
     
     </Project>


     <Project>
     
       <title>IONI</title>
       
       <frontend>php</frontend>
       
       <backend>mysql</backend>
       
       <client>Infosys</client>
       
       <team_size>5</team_size>
     
     </Project>

</Company>
*/

Recommended Answers

All 3 Replies

Why not use one of the libraries for XML processing? If you do not know much about it you should have look at Processing XML with Java (JDOM is very easy to start with)

Why not use one of the libraries for XML processing? If you do not know much about it you should have look at Processing XML with Java (JDOM is very easy to start with)

Thank you peter.In this http://www.cafeconleche.org/books/xmljava/ link different parsers are there. But i have to read xml elements on my own parser. How can i do this? Pls help me peter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.